Skip to content

Commit

Permalink
Issue #78. Changes the Backbone.sync API to enable passing through of…
Browse files Browse the repository at this point in the history
… options ... like {data} in fetch()
  • Loading branch information
jashkenas committed Dec 13, 2010
1 parent d01b136 commit 3d8fe92
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 32 deletions.
53 changes: 28 additions & 25 deletions backbone.js
Expand Up @@ -248,12 +248,13 @@
fetch : function(options) {
options || (options = {});
var model = this;
var success = function(resp) {
var success = options.success;
options.success = function(resp) {
if (!model.set(model.parse(resp), options)) return false;
if (options.success) options.success(model, resp);
if (success) success(model, resp);
};
var error = wrapError(options.error, model, options);
(this.sync || Backbone.sync)('read', this, success, error);
options.error = wrapError(options.error, model, options);
(this.sync || Backbone.sync)('read', this, options);
return this;
},

Expand All @@ -264,13 +265,14 @@
options || (options = {});
if (attrs && !this.set(attrs, options)) return false;
var model = this;
var success = function(resp) {
var success = options.success;
options.success = function(resp) {
if (!model.set(model.parse(resp), options)) return false;
if (options.success) options.success(model, resp);
if (success) success(model, resp);
};
var error = wrapError(options.error, model, options);
options.error = wrapError(options.error, model, options);
var method = this.isNew() ? 'create' : 'update';
(this.sync || Backbone.sync)(method, this, success, error);
(this.sync || Backbone.sync)(method, this, options);
return this;
},

Expand All @@ -279,12 +281,13 @@
destroy : function(options) {
options || (options = {});
var model = this;
var success = function(resp) {
var success = options.success;
options.success = function(resp) {
if (model.collection) model.collection.remove(model);
if (options.success) options.success(model, resp);
if (success) success(model, resp);
};
var error = wrapError(options.error, model, options);
(this.sync || Backbone.sync)('delete', this, success, error);
options.error = wrapError(options.error, model, options);
(this.sync || Backbone.sync)('delete', this, options);
return this;
},

Expand Down Expand Up @@ -488,12 +491,13 @@
fetch : function(options) {
options || (options = {});
var collection = this;
var success = function(resp) {
var success = options.success;
options.success = function(resp) {
collection[options.add ? 'add' : 'refresh'](collection.parse(resp));
if (options.success) options.success(collection, resp);
if (success) success(collection, resp);
};
var error = wrapError(options.error, collection, options);
(this.sync || Backbone.sync)('read', this, success, error);
options.error = wrapError(options.error, collection, options);
(this.sync || Backbone.sync)('read', this, options);
return this;
},

Expand All @@ -507,11 +511,12 @@
} else {
model.collection = coll;
}
var success = function(nextModel, resp) {
var success = options.success;
options.success = function(nextModel, resp) {
coll.add(nextModel);
if (options.success) options.success(nextModel, resp);
if (success) success(nextModel, resp);
};
return model.save(null, {success : success, error : options.error});
return model.save(null, options);
},

// **parse** converts a response into a list of models to be added to the
Expand Down Expand Up @@ -921,22 +926,20 @@
// `application/json` with the model in a param named `model`.
// Useful when interfacing with server-side languages like **PHP** that make
// it difficult to read the body of `PUT` requests.
Backbone.sync = function(method, model, success, error) {
Backbone.sync = function(method, model, options) {
var type = methodMap[method];
var modelJSON = (method === 'create' || method === 'update') ?
JSON.stringify(model.toJSON()) : null;

// Default JSON-request options.
var params = {
var params = _.extend({
url: getUrl(model) || urlError(),
type: type,
contentType: 'application/json',
data: modelJSON,
dataType: 'json',
processData: false,
success: success,
error: error
};
processData: false
}, options);

// For older servers, emulate JSON by encoding the request into an HTML-form.
if (Backbone.emulateJSON) {
Expand Down
6 changes: 3 additions & 3 deletions examples/backbone-localstorage.js
Expand Up @@ -64,7 +64,7 @@ _.extend(Store.prototype, {

// Override `Backbone.sync` to use delegate to the model or collection's
// *localStorage* property, which should be an instance of `Store`.
Backbone.sync = function(method, model, success, error) {
Backbone.sync = function(method, model, options) {

var resp;
var store = model.localStorage || model.collection.localStorage;
Expand All @@ -77,8 +77,8 @@ Backbone.sync = function(method, model, success, error) {
}

if (resp) {
success(resp);
options.success(resp);
} else {
error("Record not found");
options.error("Record not found");
}
};
9 changes: 6 additions & 3 deletions examples/todos/todos.js
Expand Up @@ -12,13 +12,16 @@ $(function(){
// Our basic **Todo** model has `content`, `order`, and `done` attributes.
window.Todo = Backbone.Model.extend({

// If you don't provide a todo, one will be provided for you.
EMPTY: "empty todo...",
// Default attributes for the todo.
defaults: {
content: "empty todo...",
done: false
},

// Ensure that each todo created has `content`.
initialize: function() {
if (!this.get("content")) {
this.set({"content": this.EMPTY});
this.set({"content": this.defaults.content});
}
},

Expand Down
8 changes: 7 additions & 1 deletion test/sync.js
Expand Up @@ -31,6 +31,13 @@ $(document).ready(function() {
ok(_.isEmpty(lastRequest.data));
});

test("sync: passing data", function() {
library.fetch({data: {a: 'a', one: 1}});
equals(lastRequest.url, '/library');
equals(lastRequest.data.a, 'a');
equals(lastRequest.data.one, 1);
});

test("sync: create", function() {
library.add(library.create(attrs));
equals(lastRequest.url, '/library');
Expand All @@ -42,7 +49,6 @@ $(document).ready(function() {
equals(data.length, 123);
});


test("sync: update", function() {
library.first().save({id: '1-the-tempest', author: 'William Shakespeare'});
equals(lastRequest.url, '/library/1-the-tempest');
Expand Down

0 comments on commit 3d8fe92

Please sign in to comment.