UNPKG

backbone.projections

Version:
279 lines (231 loc) 9.45 kB
(function(e){if("function"==typeof bootstrap)bootstrap("backboneprojections",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeBackboneProjections=e}else"undefined"!=typeof window?window.BackboneProjections=e():global.BackboneProjections=e()})(function(){var define,ses,bootstrap,module,exports; return (function(e,t,n){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require;if(!s&&o)return o(n,!0);if(r)return r(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0].call(u.exports,function(t){var r=e[n][1][t];return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require;for(var s=0;s<n.length;s++)i(n[s]);return i})({"lib/index.js":[function(require,module,exports){ // Generated by CoffeeScript 1.6.2 var extend; extend = ((typeof window !== "undefined" && window !== null ? window._ : void 0) || require('underscore')).extend; module.exports = extend({}, require('./capped'), require('./filtered'), require('./sorted')); },{"./capped":"lib/capped.js","./filtered":"lib/filtered.js","./sorted":"lib/sorted.js","underscore":"underscore"}],"lib/capped.js":[function(require,module,exports){ // Generated by CoffeeScript 1.6.2 var Collection, extend, inducedOrdering, toArray, _ref, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; Collection = ((typeof window !== "undefined" && window !== null ? window.Backbone : void 0) || require('backbone')).Collection; _ref = (typeof window !== "undefined" && window !== null ? window._ : void 0) || require('underscore'), toArray = _ref.toArray, extend = _ref.extend; inducedOrdering = function(collection) { var func; func = function(model) { return collection.indexOf(model); }; func.induced = true; return func; }; exports.Capped = (function(_super) { __extends(Capped, _super); function Capped(underlying, options) { var _this = this; if (options == null) { options = {}; } this.underlying = underlying; this.model = underlying.model; this.comparator = options.comparator || inducedOrdering(underlying); this.options = extend({ cap: 5 }, underlying.options, options); Capped.__super__.constructor.call(this, this._capped(this.underlying.models), options); this.listenTo(this.underlying, { reset: function() { return _this.reset(_this._capped(_this.underlying.models)); }, remove: function(model) { var capped; if (_this.contains(model)) { _this.remove(model); capped = _this._capped(_this.underlying.models); return _this.add(capped[_this.options.cap - 1]); } }, add: function(model) { if (_this.length < _this.options.cap) { return _this.add(model); } else if (_this.comparator(model) < _this.comparator(_this.last())) { _this.add(model); return _this.remove(_this.at(_this.options.cap)); } }, sort: function() { if (_this.comparator.induced) { return _this.reset(_this._capped(_this.underlying.models)); } } }); } Capped.prototype._capped = function(models) { var _this = this; models = toArray(models); models.sort(function(a, b) { a = _this.comparator(a); b = _this.comparator(b); if (a > b) { return 1; } else if (a < b) { return -1; } else { return 0; } }); return models.slice(0, this.options.cap); }; Capped.prototype.resize = function(cap) { var capped, idx, model, _i, _ref1; if (this.options.cap > cap) { this.options.cap = cap; _ref1 = this.models; for (idx = _i = _ref1.length - 1; _i >= 0; idx = _i += -1) { model = _ref1[idx]; if (idx < cap) { break; } this.remove(model); } } else if (this.options.cap < cap) { this.options.cap = cap; capped = this._capped(this.underlying.models); this.add(capped.slice(this.length, this.options.cap)); } return this.trigger('resize'); }; return Capped; })(Collection); exports.CappedCollection = exports.Capped; },{"backbone":"backbone","underscore":"underscore"}],"lib/filtered.js":[function(require,module,exports){ // Generated by CoffeeScript 1.6.2 var Collection, extend, inducedOrdering, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; Collection = ((typeof window !== "undefined" && window !== null ? window.Backbone : void 0) || require('backbone')).Collection; extend = ((typeof window !== "undefined" && window !== null ? window._ : void 0) || require('underscore')).extend; inducedOrdering = function(collection) { var func; func = function(model) { return collection.indexOf(model); }; func.induced = true; return func; }; exports.Filtered = (function(_super) { __extends(Filtered, _super); function Filtered(underlying, options) { var _this = this; if (options == null) { options = {}; } this.underlying = underlying; this.model = underlying.model; this.comparator = options.comparator || inducedOrdering(underlying); this.options = extend({}, underlying.options, options); Filtered.__super__.constructor.call(this, this.underlying.models.filter(this.options.filter), options); this.listenTo(this.underlying, { reset: function() { return _this.reset(_this.underlying.models.filter(_this.options.filter)); }, remove: function(model) { if (_this.contains(model)) { return _this.remove(model); } }, add: function(model) { if (_this.options.filter(model)) { return _this.add(model); } }, change: function(model) { return _this.decideOn(model); }, sort: function() { if (_this.comparator.induced) { return _this.sort(); } } }); } Filtered.prototype.update = function() { var model, _i, _len, _ref, _results; _ref = this.underlying.models; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { model = _ref[_i]; _results.push(this.decideOn(model)); } return _results; }; Filtered.prototype.decideOn = function(model) { if (this.contains(model)) { if (!this.options.filter(model)) { return this.remove(model); } } else { if (this.options.filter(model)) { return this.add(model); } } }; return Filtered; })(Collection); exports.FilteredCollection = exports.Filtered; },{"backbone":"backbone","underscore":"underscore"}],"lib/sorted.js":[function(require,module,exports){ // Generated by CoffeeScript 1.6.2 var Collection, extend, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; Collection = ((typeof window !== "undefined" && window !== null ? window.Backbone : void 0) || require('backbone')).Collection; extend = ((typeof window !== "undefined" && window !== null ? window._ : void 0) || require('underscore')).extend; exports.Sorted = (function(_super) { __extends(Sorted, _super); function Sorted(underlying, options) { var _this = this; if (options == null) { options = {}; } if (!options.comparator) { throw new Error("provide a comparator"); } this.underlying = underlying; this.model = underlying.model; this.comparator = options.comparator; this.options = extend({}, underlying.options, options); Sorted.__super__.constructor.call(this, this.underlying.models, options); this.listenTo(this.underlying, { reset: function() { return _this.reset(_this.underlying.models); }, remove: function(model) { return _this.remove(model); }, add: function(model) { return _this.add(model); } }); } return Sorted; })(Collection); exports.Reversed = (function(_super) { __extends(Reversed, _super); function Reversed(underlying, options) { if (options == null) { options = {}; } options.comparator = function(model) { return -underlying.indexOf(model); }; Reversed.__super__.constructor.call(this, underlying, options); this.listenTo(this.underlying, { sort: this.sort }); } return Reversed; })(exports.Sorted); exports.SortedCollection = exports.Sorted; exports.ReversedCollection = exports.Reversed; },{"backbone":"backbone","underscore":"underscore"}]},{},["lib/index.js"])('lib/index.js') });