binding the comparator function before using it, so that you can rely on properties of your collection within it.

This commit is contained in:
Jeremy Ashkenas
2012-01-12 15:13:22 -05:00
parent a8dbe4efa7
commit 1d90bb9cfc
3 changed files with 15 additions and 3 deletions

View File

@@ -217,6 +217,18 @@ $(document).ready(function() {
equals(col.indexOf(tom), 2);
});
test("Collection: comparator that depends on `this`", function() {
var col = new Backbone.Collection;
col.negative = function(num) {
return -num;
};
col.comparator = function(a) {
return this.negative(a.id);
};
col.add([{id: 1}, {id: 2}, {id: 3}]);
equals(col.pluck('id').join(' '), '3 2 1');
});
test("Collection: remove", function() {
var removed = otherRemoved = null;
col.bind('remove', function(model){ removed = model.get('label'); });

View File

@@ -318,7 +318,6 @@ $(document).ready(function() {
options.success.call(this, {admin: true});
};
model.save(null, {error: function(model, error) {
console.log('erroring!');
lastError = error;
}});