Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added options to sort() and _sort() #1005

Merged
merged 3 commits into from
Jul 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/app/js/model-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {
`reset` event.
@param {Boolean} [options.silent=false] If `true`, no `reset` event will
be fired.
@param {Boolean} [options.descending=false] If `true`, sorts in descending order
@chainable
**/
sort: function (options) {
Expand All @@ -825,7 +826,7 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {

options || (options = {});

models.sort(Y.bind(this._sort, this));
models.sort(Y.rbind(this._sort, this, options));

facade = Y.merge(options, {
models: models,
Expand Down Expand Up @@ -1122,11 +1123,19 @@ Y.ModelList = Y.extend(ModelList, Y.Base, {
@method _sort
@param {Model} a First model to compare.
@param {Model} b Second model to compare.
@return {Number} `-1` if _a_ is less than _b_, `0` if equal, `1` if greater.
@param {Object} [options] Options passed from `sort()` function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably document the options param on the sort() method as well.

@param {Boolean} [options.descending=false] If `true`, sorts in descending order
@return {Number} `-1` if _a_ is less than _b_, `0` if equal, `1` if greater (for ascending).
@protected
**/
_sort: function (a, b) {
return this._compare(this.comparator(a), this.comparator(b));
_sort: function (a, b, options) {
var result = this._compare(this.comparator(a), this.comparator(b));

if (!result) {
return result;
}

return options && options.descending ? -result : result;
},

// -- Event Handlers -------------------------------------------------------
Expand Down
16 changes: 16 additions & 0 deletions src/app/tests/unit/assets/model-list-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,22 @@ modelListSuite.add(new Y.Test.Case({

},

'sort({descending:true}) should re-sort the list': function () {
var list = this.createList();

list.add([{foo: 'z'}, {foo: 'a'}, {foo: 'x'}, {foo: 'y'}]);

ArrayAssert.itemsAreSame(['z', 'a', 'x', 'y'], list.get('foo'));

list.comparator = function (model) {
return model.get('foo');
};

Assert.areSame(list, list.sort({descending:true}), 'sort() should be chainable');
ArrayAssert.itemsAreSame(['z', 'y', 'x', 'a'], list.get('foo'));

},

'sync() should just call the supplied callback by default': function () {
var calls = 0,
list = this.createList();
Expand Down