mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-30 03:00:06 -04:00
Issue #78. Changes the Backbone.sync API to enable passing through of options ... like {data} in fetch()
This commit is contained in:
53
backbone.js
53
backbone.js
@@ -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;
|
||||
},
|
||||
|
||||
@@ -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;
|
||||
},
|
||||
|
||||
@@ -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;
|
||||
},
|
||||
|
||||
@@ -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;
|
||||
},
|
||||
|
||||
@@ -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
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
@@ -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");
|
||||
}
|
||||
};
|
||||
@@ -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});
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -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');
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user