Anton Shestakov <av6@dwimlabs.net>, Fri, 05 Feb 2016 16:50:04 +0800
"release" the long overdue 0.4.0
static/js/backbone.shard.js
Permissions: -rw-r--r--
* (c) 2013 Anton Shestakov. * This extension to Backbone may be freely distributed * http://opensource.org/licenses/mit-license.php Backbone.Shard = function(options) { this._collection = options.collection; this._filter = options.filter; this.models = this._collection.filter(this._filter); .on('all', function(event, model) { if (this._filter(model) && _(this.models).contains(model)) { this.trigger.apply(this, arguments); if (this._filter(model) && !_(this.models).contains(model)) { this.trigger('add', model); if (!this._filter(model) && _(this.models).contains(model)) { this.models = _.without(this.models, model); this.trigger('remove', model); } else if (/^change:/.test(event) && this._filter(model) && _(this.models).contains(model)) { this.trigger.apply(this, arguments); .on('add', function(model) { if (this._filter(model) && !_(this.models).contains(model)) { this.trigger('add', model); .on('remove', function(model) { if (this._filter(model) && _(this.models).contains(model)) { this.models = _.without(this.models, model); this.trigger('remove', model); .on('reset', function() { this.models = this._collection.filter(this._filter); this.initialize.apply(this, arguments); _.extend(Backbone.Shard.prototype, Backbone.Events, { initialize: function() {}, return _.map(this.models, function(model) { return model.get(attr); }); // Underscore methods that we want to implement on the Shard. 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', 'toArray', 'size', 'first', 'head', 'take', 'initial', 'rest', 'tail', 'drop', 'last', 'without', 'indexOf', 'shuffle', 'lastIndexOf', // Mix in each Underscore method as a proxy to `Shard#models`. _.each(methods, function(method) { Backbone.Shard.prototype[method] = function() { 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 _[method](this.models, iterator, context);