Login Register

Problems overriding constructor when extending SortedList

I'm trying to extend dojox.collections.SortedList to allow an ascending or descending sort order based on a boolean passed into the constructor. This is my first go at extending a widget, so I'm hitting a few life cycle problems.

My code is :

// SortedList supporting ascending and descending sort
dojo.provide("gwp.SortedListTwoWay");
dojo.require("dojox.collections._base");
dojo.declare("gwp.SortedListTwoWay", dojox.collections.SortedList, {
	sortAsc: true,
	constructor: function(/*object*/dictionary, /*boolean*/ sortAscending)
	{
		this.inherited([dictionary]);
		if (sortAscending) this.sortAsc = sortAscending;
	},
	sorter: function(a,b){
		if (this.sortAsc){
			if (a.key > b.key) return 1;
			if (a.key < b.key) return -1;
			return 0;
		} else {
			if (a.key > b.key) return -1;
			if (a.key < b.key) return 1;
			return 0;
		}
	}
});

With the call to the constructor in my code as

GWPGlobal.personStoreItems = new gwp.SortedListTwoWay(null, false);

And the error that this gives me is :
TypeError: args has no properties message=args has no properties

I think my problem is in the call to this.inherited(), but after a day of head scratching and trying to follow source code, I'm stumped. Any help would be much appreciated.

your call to inherited() is b0rken

Use:

this.inherited(arguments);

and pass the sortAsc property through (it'll get mixed in by your superclass anyway). You can shorten the entire thing to be:

// SortedList supporting ascending and descending sort
dojo.provide("gwp.SortedListTwoWay");
dojo.require("dojox.collections._base");
dojo.declare("gwp.SortedListTwoWay", dojox.collections.SortedList, {
        sortAsc: true,
        sorter: function(a,b){
                if (this.sortAsc){
                        if (a.key > b.key) return 1;
                        if (a.key < b.key) return -1;
                        return 0;
                } else {
                        if (a.key > b.key) return -1;
                        if (a.key < b.key) return 1;
                        return 0;
                }
        }
});

// usage:
GWPGlobal.personStoreItems = new gwp.SortedListTwoWay({ sortAsc: false });
--
Project Lead, The Dojo Toolkit
President, The Dojo Foundation

dictionary not set in superclass?

Thanks Alex - I've tried the above changes exactly as you spec out, but this gives a different error - one which I've experienced before (it was actually the reason I was trying to pass dictionary to the inherited constructor:

TypeError: dictionary.getIterator is not a function message=dictionary.getIterator is not a function

Any ideas?

Thanks, G.