[Pkg-javascript-commits] [backbone] 25/211: Issue #78. Changes the Backbone.sync API to enable passing through of options ... like {data} in fetch()
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 16:59:58 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.5.0
in repository backbone.
commit 3d8fe92f1e5447acf4d76d33dd62bf368ee4b523
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Mon Dec 13 10:15:09 2010 -0500
Issue #78. Changes the Backbone.sync API to enable passing through of options ... like {data} in fetch()
---
backbone.js | 53 +++++++++++++++++++++------------------
examples/backbone-localstorage.js | 6 ++---
examples/todos/todos.js | 9 ++++---
test/sync.js | 8 +++++-
4 files changed, 44 insertions(+), 32 deletions(-)
diff --git a/backbone.js b/backbone.js
index c422b6d..beb4904 100644
--- a/backbone.js
+++ b/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) {
diff --git a/examples/backbone-localstorage.js b/examples/backbone-localstorage.js
index add3cf7..091d7f3 100644
--- a/examples/backbone-localstorage.js
+++ b/examples/backbone-localstorage.js
@@ -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");
}
};
\ No newline at end of file
diff --git a/examples/todos/todos.js b/examples/todos/todos.js
index e4a7fa4..3e2ef6b 100644
--- a/examples/todos/todos.js
+++ b/examples/todos/todos.js
@@ -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});
}
},
diff --git a/test/sync.js b/test/sync.js
index 0d21b23..02921e9 100644
--- a/test/sync.js
+++ b/test/sync.js
@@ -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');
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git
More information about the Pkg-javascript-commits
mailing list