UNPKG

ampersand-collection-rest-mixin

Version:
127 lines (108 loc) 4.48 kB
/*$AMPERSAND_VERSION*/ var sync = require('ampersand-sync'); var assign = require('lodash/assign'); module.exports = { // Fetch the default set of models for this collection, resetting the // collection when they arrive. If `reset: true` is passed, the response // data will be passed through the `reset` method instead of `set`. fetch: function(options) { options = options ? assign({}, options) : {}; if (options.parse === void 0) options.parse = true; var self = this; var success = options.success; options.success = function(resp) { var method = options.reset ? 'reset' : 'set'; if (options.set !== false) self[method](resp, options); if (success) success(self, resp, options); if (options.set !== false) self.trigger('sync', self, resp, options); }; // Wrap an optional error callback with a fallback error event. var error = options.error; options.error = function(resp) { if (error) error(self, resp, options); self.trigger('error', self, resp, options); }; var request = this.sync('read', this, options); // Make the request available on the options object so it can be accessed // further down the line by `parse`, sync listeners, etc // https://github.com/AmpersandJS/ampersand-collection-rest-mixin/commit/d32d788aaff912387eb1106f2d7ad183ec39e11a#diff-84c84703169bf5017b1bc323653acaa3R32 options.xhr = request; return request; }, // Create a new instance of a model in this collection. Add the model to the // collection immediately, unless `wait: true` is passed, in which case we // wait for the server to agree. create: function(model, options) { options = options ? assign({}, options) : {}; if (!(model = this._prepareModel(model, options))) return false; if (!options.wait) this.add(model, options); var self = this; var success = options.success; options.success = function(model, resp) { if (options.wait) self.add(model, options); if (success) success(model, resp, options); }; model.save(null, options); return model; }, sync: function() { return sync.apply(this, arguments); }, // Get or fetch a model by Id. getOrFetch: function (id, options, cb) { if (arguments.length !== 3) { cb = options; options = {}; } var self = this; var model = this.get(id); if (model) { return window.setTimeout(cb.bind(null, null, model), 0); } if (options.all) { //preserve original `options.always` var always = options.always; options.always = function(err, resp, body) { if (always) always(err, resp, body); if (!cb) return; var model = self.get(id); var err2 = model ? null : new Error('not found'); cb(err2, model); }; return this.fetch(options); } else { return this.fetchById(id, options, cb); } }, // fetchById: fetches a model and adds it to // collection when fetched. fetchById: function (id, options, cb) { if (arguments.length !== 3) { cb = options; options = {}; } var self = this; var idObj = {}; idObj[this.mainIndex] = id; var model = new this.model(idObj, {collection: this}); //preserve original `options.success` var success = options.success; options.success = function (resp) { model = self.add(model); if (success) success(self, resp, options); if (cb) cb(null, model); }; //preserve original `options.error` var error = options.error; options.error = function (collection, resp) { delete model.collection; if (error) error(collection, resp, options); if (cb) { var err = new Error(resp.rawRequest.statusText); err.status = resp.rawRequest.status; cb(err); } }; return model.fetch(options); } };