--- a/backbone.shard.js Thu Dec 13 04:02:52 2012 +0900
+++ b/backbone.shard.js Fri Dec 14 23:54:33 2012 +0900
// Underscore methods that we want to implement on the Shard.
-var methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find',
- 'detect', 'filter', 'select', 'reject', 'every', 'all', 'some', 'any',
- 'include', 'contains', 'invoke', 'max', 'min', 'sortBy', 'sortedIndex',
- 'toArray', 'size', 'first', 'initial', 'rest', 'last', 'without', 'indexOf',
- 'shuffle', 'lastIndexOf', 'isEmpty', 'groupBy'];
+var methods = ['forEach', 'each', 'map', 'collect', 'reduce', 'foldl',
+ 'inject', 'reduceRight', 'foldr', 'find', 'detect', 'filter', 'select',
+ 'reject', 'every', 'all', 'some', 'any', 'include', 'contains', 'invoke'
+ , 'max', 'min', 'sortedIndex', 'toArray', 'size', 'first', 'head',
+ 'take', 'initial', 'rest', 'tail', 'last', 'without', 'indexOf',
+ 'shuffle', 'lastIndexOf', 'isEmpty'];
// Mix in each Underscore method as a proxy to `Shard#models`.
_.each(methods, function(method) {
Backbone.Shard.prototype[method] = function() {
- return _[method].apply(_, [this.models].concat(_.toArray(arguments)));
+ var args = Array.prototype.slice.call(arguments);
+ args.unshift(this.models);
+ return _[method].apply(_, args);
+// Underscore methods that take a property name as an argument.
+var attributeMethods = ['groupBy', 'countBy', 'sortBy'];
+// Use attributes instead of properties.
+_.each(attributeMethods, function(method) {
+ Backbone.Shard.prototype[method] = function(value, context) {
+ var iterator = _.isFunction(value) ? value : function(model) {
+ return model.get(value);
+ return _[method](this.models, iterator, context);
--- a/test/underscore-methods.js Thu Dec 13 04:02:52 2012 +0900
+++ b/test/underscore-methods.js Fri Dec 14 23:54:33 2012 +0900
return memo += model.get('data');
}, ''), 'cba', 'reduceRight() works');
+test('Underscore methods: method aliases (working in Backbone.Collection since 0.9.9)', function() {
+ strictEqual(shard.head(), a, 'head() works');
+ deepEqual(shard.tail(), [b, c], 'tail() works');
+ deepEqual(shard.countBy('data'), {a: 1, b: 1, c: 1}, 'countBy() works');
+ deepEqual(shard.select(function(model) {
+ return model.get('data') != 'b';
+ }), [a, c], 'select() works');
+ equal(shard.foldl(function(memo, model) {
+ return memo += model.get('data');
+ }, ''), 'abc', 'foldl() works');