Skip to content

Commit

Permalink
fixes jashkenas#928 - Save sends correct attrs.
Browse files Browse the repository at this point in the history
* Temporarily set model's attrs for `sync`.
* Remove cross-module (global) dependencies in
  Collection, Model, and sync test modules.
  • Loading branch information
braddunbar committed Feb 2, 2012
1 parent c4254cb commit ec61503
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 31 deletions.
12 changes: 9 additions & 3 deletions backbone.js
Expand Up @@ -289,7 +289,7 @@
// If the server returns an attributes hash that differs, the model's
// state will be `set` again.
save: function(key, value, options) {
var attrs;
var attrs, current;
if (_.isObject(key) || key == null) {
attrs = key;
options = value;
Expand All @@ -299,7 +299,11 @@
}

options = options ? _.clone(options) : {};
if (attrs && !this[options.wait ? '_validate' : 'set'](attrs, options)) return false;
if (options.wait) current = _.clone(this.attributes);
var silentOptions = _.extend({}, options, {silent: true});
if (attrs && !this.set(attrs, options.wait ? silentOptions : options)) {
return false;
}
var model = this;
var success = options.success;
options.success = function(resp, status, xhr) {
Expand All @@ -314,7 +318,9 @@
};
options.error = Backbone.wrapError(options.error, model, options);
var method = this.isNew() ? 'create' : 'update';
return (this.sync || Backbone.sync).call(this, method, this, options);
var xhr = (this.sync || Backbone.sync).call(this, method, this, options);
if (options.wait) this.set(current, silentOptions);
return xhr;
},

// Destroy this model on the server if it was already persisted.
Expand Down
19 changes: 14 additions & 5 deletions test/collection.js
@@ -1,12 +1,21 @@
$(document).ready(function() {

module("Backbone.Collection");
var lastRequest = null;
var sync = Backbone.sync;

window.lastRequest = null;
module("Backbone.Collection", {

Backbone.sync = function() {
lastRequest = _.toArray(arguments);
};
setup: function() {
Backbone.sync = function() {
lastRequest = _.toArray(arguments);
};
},

teardown: function() {
Backbone.sync = sync;
}

});

var a = new Backbone.Model({id: 3, label: 'a'});
var b = new Backbone.Model({id: 2, label: 'b'});
Expand Down
56 changes: 40 additions & 16 deletions test/model.js
@@ -1,16 +1,32 @@
$(document).ready(function() {

module("Backbone.Model");

// Variable to catch the last request.
window.lastRequest = null;

window.originalSync = Backbone.sync;
var lastRequest = null;
// Variable to catch ajax params.
var ajaxParams = null;
var sync = Backbone.sync;
var ajax = $.ajax;
var urlRoot = null;

module("Backbone.Model", {

setup: function() {
Backbone.sync = function() {
lastRequest = _.toArray(arguments);
sync.apply(this, arguments);
};
$.ajax = function(params) { ajaxParams = params; };
urlRoot = Backbone.Model.prototype.urlRoot;
Backbone.Model.prototype.urlRoot = '/';
},

teardown: function() {
Backbone.sync = sync;
$.ajax = ajax;
Backbone.Model.prototype.urlRoot = urlRoot;
}

// Stub out Backbone.request...
Backbone.sync = function() {
lastRequest = _.toArray(arguments);
};
});

var attrs = {
id : '1-the-tempest',
Expand Down Expand Up @@ -67,13 +83,8 @@ $(document).ready(function() {
doc.collection.url = '/collection/';
equal(doc.url(), '/collection/1-the-tempest');
doc.collection = null;
var failed = false;
try {
doc.url();
} catch (e) {
failed = true;
}
equal(failed, true);
doc.urlRoot = null;
raises(function() { doc.url(); });
doc.collection = collection;
});

Expand Down Expand Up @@ -603,4 +614,17 @@ $(document).ready(function() {
equal(model.previous(''), true);
});

test("`save` with `wait` sends correct attributes", function() {
var changed = 0;
var model = new Backbone.Model({x: 1, y: 2});
model.on('change:x', function() { changed++; });
model.save({x: 3}, {wait: true});
deepEqual(JSON.parse(ajaxParams.data), {x: 3, y: 2});
equal(model.get('x'), 1);
equal(changed, 0);
lastRequest[2].success({});
equal(model.get('x'), 3);
equal(changed, 1);
});

});
23 changes: 16 additions & 7 deletions test/sync.js
@@ -1,11 +1,21 @@
$(document).ready(function() {

module("Backbone.sync", {setup : function() {
window.lastRequest = null;
$.ajax = function(obj) {
lastRequest = obj;
};
}});
var ajax = $.ajax
var lastRequest = null;

module("Backbone.sync", {

setup : function() {
$.ajax = function(obj) {
lastRequest = obj;
};
},

teardown: function() {
$.ajax = ajax;
}

});

var Library = Backbone.Collection.extend({
url : function() { return '/library'; }
Expand All @@ -20,7 +30,6 @@ $(document).ready(function() {
};

test("sync: read", function() {
Backbone.sync = originalSync;
library.fetch();
equal(lastRequest.url, '/library');
equal(lastRequest.type, 'GET');
Expand Down

0 comments on commit ec61503

Please sign in to comment.