Adding chain() to the Collection prototype

This commit is contained in:
Nick Fitzgerald
2010-10-19 12:28:05 -07:00
committed by Jeremy Ashkenas
parent c98cfeb4f2
commit 8d7eff8cd2
2 changed files with 12 additions and 0 deletions

View File

@@ -425,6 +425,13 @@
return model.save(null, {success : success, error : options.error});
},
// Proxy to _'s chain. Can't be proxied the same way the rest of the
// underscore methods are proxied because it relies on the underscore
// constructor.
chain: function () {
return _(this.models).chain();
},
// Reset all internal state. Called when the collection is refreshed.
_reset : function(options) {
this.length = 0;

View File

@@ -95,6 +95,11 @@ $(document).ready(function() {
ok(!_.include(col.without(d)), d);
equals(col.max(function(model){ return model.id; }).id, 4);
equals(col.min(function(model){ return model.id; }).id, 1);
same(col.chain()
.filter(function(o){ return o.id % 2 === 0; })
.map(function(o){ return o.id * 2; })
.value(),
[8, 4]);
});
test("Collection: refresh", function() {