Skip to content

Commit

Permalink
Make it possible to take advantage of jQuery.Deferred with Backbone, …
Browse files Browse the repository at this point in the history
…without breaking compatibility by changing return values.

Implemented by adding a "promise" attribute to Backbone.Model and Backbone.Collection, set by Backbone.Model's "fetch", "save", "destroy" and Backbone.Collectin's "fetch" and "create".
  • Loading branch information
PaulUithol committed Mar 22, 2011
1 parent da52ae1 commit ed5a88d
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@
// CouchDB users may want to set this to `"_id"`.
idAttribute : 'id',

// A jQuery promise object (set on 'fetch', 'save' and 'destroy')
promise: null,

// Initialize is an empty function by default. Override it with your own
// initialization logic.
initialize : function(){},
Expand Down Expand Up @@ -264,7 +267,8 @@
if (success) success(model, resp);
};
options.error = wrapError(options.error, model, options);
(this.sync || Backbone.sync).call(this, 'read', this, options);
var request = (this.sync || Backbone.sync).call(this, 'read', this, options);
this.promise = _.isFunction( request.promise ) ? request.promise() : request;
return this;
},

Expand All @@ -282,7 +286,8 @@
};
options.error = wrapError(options.error, model, options);
var method = this.isNew() ? 'create' : 'update';
(this.sync || Backbone.sync).call(this, method, this, options);
var request = (this.sync || Backbone.sync).call(this, method, this, options);
this.promise = _.isFunction( request.promise ) ? request.promise() : request;
return this;
},

Expand All @@ -297,7 +302,8 @@
if (success) success(model, resp);
};
options.error = wrapError(options.error, model, options);
(this.sync || Backbone.sync).call(this, 'delete', this, options);
var request = (this.sync || Backbone.sync).call(this, 'delete', this, options);
this.promise = _.isFunction( request.promise ) ? request.promise() : request;
return this;
},

Expand Down Expand Up @@ -415,6 +421,9 @@
// This should be overridden in most cases.
model : Backbone.Model,

// A jQuery promise object (set on 'fetch')
promise: null,

// Initialize is an empty function by default. Override it with your own
// initialization logic.
initialize : function(){},
Expand Down Expand Up @@ -507,7 +516,8 @@
if (success) success(collection, resp);
};
options.error = wrapError(options.error, collection, options);
(this.sync || Backbone.sync).call(this, 'read', this, options);
var request = (this.sync || Backbone.sync).call(this, 'read', this, options);
this.promise = _.isFunction( request.promise ) ? request.promise() : request;
return this;
},

Expand All @@ -528,6 +538,7 @@
coll.add(nextModel);
if (success) success(nextModel, resp);
};
this.promise = model.promise;
return model.save(null, options);
},

Expand Down Expand Up @@ -1006,7 +1017,7 @@
}

// Make the request.
$.ajax(params);
return $.ajax(params);
};

// Helpers
Expand Down

0 comments on commit ed5a88d

Please sign in to comment.