From 8d7eff8cd28d1d5960bfdf4257a6ee0401849414 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Tue, 19 Oct 2010 12:28:05 -0700 Subject: [PATCH] Adding chain() to the Collection prototype --- backbone.js | 7 +++++++ test/collection.js | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/backbone.js b/backbone.js index 638cc916..d53e3f58 100644 --- a/backbone.js +++ b/backbone.js @@ -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; diff --git a/test/collection.js b/test/collection.js index de5c974f..ae0cd3c8 100644 --- a/test/collection.js +++ b/test/collection.js @@ -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() {