Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let fetchById accept options #23

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions ampersand-collection-rest-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ 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 = {};
}
var self = this;
var model = this.get(id);
if (model) return cb(null, model);

function done() {
var model = self.get(id);
if (model) {
Expand All @@ -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'));
}
});
}));
}
};