UNPKG

marbles

Version:

Front-end framework for routing, http, and data handling

364 lines (302 loc) 7.88 kB
"use strict"; var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { "default": obj }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* @flow weak */ var _Utils = require("./utils"); var _Utils2 = _interopRequireWildcard(_Utils); var _Model = require("./model"); var _Model2 = _interopRequireWildcard(_Model); var _Accessors = require("./accessors"); var _Accessors2 = _interopRequireWildcard(_Accessors); var _Events = require("./events"); var _Events2 = _interopRequireWildcard(_Events); var _CIDMapping = require("./id_mapping"); var _CIDMapping2 = _interopRequireWildcard(_CIDMapping); /** * @deprecated Use the Store instead * @see Marbles.Store * @memberof Marbles * @class */ var Collection = _Utils2["default"].createClass({ displayName: "Marbles.Collection", mixins: [ // Add some properties to ctor { ctor: { collectionName: "collection", cidScope: ["collectionName"], model: _Model2["default"], buildModel: function buildModel(attrs, options) { var ModelCtor = (options || {}).model || this.model, _instance = ModelCtor.find(attrs, { fetch: false }); if (_instance) { _instance.parseAttributes(attrs); } else { _instance = new ModelCtor(attrs); } return _instance; } } }, // Make ctor and proto evented { ctor: _Events2["default"], proto: _Events2["default"] }, // Extend ptoto with accessor methods _Accessors2["default"], // CIDMapping extends both ctor and proto _CIDMapping2["default"]], willInitialize: function willInitialize(options) { this.modelCIDs = []; this.options = { unique: !!options.unique }; if (options.cid) { this.cid = options.cid; } this.initCIDMapping(); this.watchModelMortality(); this.on("reset prepend append remove", function () { this.trigger("change:models", this.models); }, this); }, watchModelMortality: function watchModelMortality() { this.constructor.model.on("detach", function (cid) { this.removeCIDs([cid]); }, this); }, indexOf: function indexOf(model) { return this.modelCIDs.indexOf(model.cid); }, first: function first() { return this.constructor.model.find({ cid: this.modelCIDs[0] }); }, last: function last() { return this.constructor.model.find({ cid: this.modelCIDs[this.modelCIDs.length - 1] }); }, forEach: function forEach(callback, thisArg) { var _ref = this.modelCIDs, model; for (var i = 0, _len = _ref.length; i < _len; i++) { model = this.constructor.model.find({ cid: _ref[i] }); if (model) { callback.call(thisArg || this, model, i); } } }, models: function models() { var _ref = this.modelCIDs, models = [], model; for (var i = 0, _len = _ref.length; i < _len; i++) { model = this.constructor.model.find({ cid: _ref[i] }); if (model) { models.push(model); } } return models; }, resetJSON: function resetJSON(json, options) { if (!options) { options = {}; } this.modelCIDs = []; var models = this.appendJSON(json, { silent: true }); if (!options.silent) { this.trigger("reset", models); } return models; }, resetModels: function resetModels(models, options) { if (!options) { options = {}; } this.modelCIDs = []; models = this.appendModels(models, { silent: true }); if (!options.silent) { this.trigger("reset", models); } return models; }, reset: function reset(options) { if (!options) { options = {}; } this.modelCIDs = []; if (!options.silent) { this.trigger("reset", []); } }, removeAtIndex: function removeAtIndex(cidIndex) { var cid = this.modelCIDs[cidIndex]; this.modelCIDs = this.modelCIDs.slice(0, cidIndex).concat(this.modelCIDs.slice(cidIndex + 1, this.modelCIDs.length)); this.trigger("remove", cid); return this.modelCIDs.length; }, removeCIDs: function removeCIDs(cids) { var index; for (var i = 0, _len = cids.length; i < _len; i++) { index = this.modelCIDs.indexOf(cids[i]); if (index === -1) { continue; } this.removeAtIndex(index); } return this.modelCIDs.length; }, remove: function remove() { var models = Array.prototype.slice.call(arguments, 0); for (var i = 0, _len = models.length; i < _len; i++) { this.removeCIDs([models[i].cid]); } return this.modelCIDs.length; }, prependJSON: function prependJSON(json, options) { if (!json || !json.length) { return []; } if (!options) { options = {}; } var models = [], model; for (var i = json.length - 1; i >= 0; i--) { model = this.constructor.buildModel(json[i]); if (!this.options.unique || this.modelCIDs.indexOf(model.cid) === -1) { this.modelCIDs.unshift(model.cid); models.unshift(model); } } if (!options.silent) { this.trigger("prepend", models); } return models; }, prependModels: function prependModels(models, options) { if (!models || !models.length) { return []; } if (!options) { options = {}; } var acceptedModels = [], model; for (var i = models.length - 1; i >= 0; i--) { model = models[i]; if (!this.options.unique || this.modelCIDs.indexOf(model.cid) === -1) { this.modelCIDs.unshift(model.cid); acceptedModels.unshift(model); } } if (!options.silent) { this.trigger("prepend", acceptedModels); } return acceptedModels; }, prependCIDs: function prependCIDs(cids, options) { if (!cids || !cids.length) { return []; } if (!options) { options = {}; } var acceptedModels = [], cid, model; for (var i = cids.length; i >= 0; i--) { cid = cids[i]; if (!this.options.unique || this.modelCIDs.indexOf(cid) === -1) { model = this.constructor.find({ cid: cid }); if (!model) { continue; } this.modelCIDs.unshift(cid); acceptedModels.unshift(model); } } if (!options.silent) { this.trigger("prepend", acceptedModels); } return acceptedModels; }, unshift: function unshift() { return this.prependModels(Array.prototype.slice.call(arguments, 0)); }, appendJSON: function appendJSON(json, options) { if (!json || !json.length) { return []; } if (!options) { options = {}; } var models = [], model; for (var i = 0, _len = json.length; i < _len; i++) { model = this.constructor.buildModel(json[i]); if (!this.options.unique || this.modelCIDs.indexOf(model.cid) === -1) { this.modelCIDs.push(model.cid); models.push(model); } } if (!options.silent) { this.trigger("append", models); } return models; }, appendModels: function appendModels(models, options) { if (!models || !models.length) { return []; } if (!options) { options = {}; } var acceptedModels = [], model; for (var i = 0, _len = models.length; i < _len; i++) { model = models[i]; if (!this.options.unique || this.modelCIDs.indexOf(model.cid) === -1) { this.modelCIDs.push(model.cid); acceptedModels.push(model); } } if (!options.silent) { this.trigger("append", acceptedModels); } return acceptedModels; }, appendCIDs: function appendCIDs(cids, options) { if (!cids || !cids.length) { return []; } if (!options) { options = {}; } var acceptedModels = [], cid, model; for (var i = 0, _len = cids.length; i < _len; i++) { cid = cids[i]; if (!this.options.unique || this.modelCIDs.indexOf(cid) === -1) { model = this.constructor.find({ cid: cid }); if (!model) { continue; } this.modelCIDs.push(cid); acceptedModels.push(model); } } if (!options.silent) { this.trigger("append", acceptedModels); } return acceptedModels; }, push: function push() { return this.appendModels(Array.prototype.slice.call(arguments, 0)); } }); exports["default"] = Collection; module.exports = exports["default"];