From 55a59a56115c7b833da87c2574c598b72b4d02ea Mon Sep 17 00:00:00 2001 From: Gilad Peleg Date: Fri, 3 Apr 2015 19:32:28 +0300 Subject: [PATCH] let fetchById accept options - closes https://github.com/AmpersandJS/ampersand-rest-collection/issues/18 --- ampersand-collection-rest-mixin.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/ampersand-collection-rest-mixin.js b/ampersand-collection-rest-mixin.js index 3c3903c..c4fd262 100644 --- a/ampersand-collection-rest-mixin.js +++ b/ampersand-collection-rest-mixin.js @@ -53,8 +53,7 @@ module.exports = { }, // Get or fetch a model by Id. - getOrFetch: function (id, options, cb) { - + getOrFetch: function(id, options, cb) { if (arguments.length !== 3) { cb = options; options = {}; @@ -62,6 +61,7 @@ module.exports = { var self = this; var model = this.get(id); if (model) return cb(null, model); + function done() { var model = self.get(id); if (model) { @@ -75,26 +75,30 @@ module.exports = { options.error = done; return this.fetch(options); } else { - return this.fetchById(id, cb); + return this.fetchById(id, options, cb); } }, // fetchById: fetches a model and adds it to // collection when fetched. - fetchById: function (id, cb) { + fetchById: function(id, options, cb) { + if (arguments.length !== 3) { + cb = options; + options = {}; + } var self = this; var idObj = {}; idObj[this.model.prototype.idAttribute] = id; var model = new this.model(idObj, {collection: this}); - return model.fetch({ - success: function () { + return model.fetch(assign(options || {}, { + success: function() { self.add(model); if (cb) cb(null, model); }, - error: function () { + error: function() { delete model.collection; if (cb) cb(Error('not found')); } - }); + })); } };