Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@

// Return a copy of the model's `attributes` object.
toJSON: function(options) {
return _.clone(this.attributes);
options || (options = {});
return options.attrs ? options.attrs : _.clone(this.attributes);
},

// Proxy `Backbone.sync` by default -- but override this if you need
Expand Down Expand Up @@ -1573,7 +1574,7 @@
// Ensure that we have the appropriate request data.
if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) {
params.contentType = 'application/json';
params.data = JSON.stringify(options.attrs || model.toJSON(options));
params.data = JSON.stringify(model.toJSON(options));
}

// For older servers, emulate JSON by encoding the request into an HTML-form.
Expand Down
13 changes: 13 additions & 0 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,19 @@
model.save({x: 1}, {wait: true});
});

QUnit.test('toJSON is called during save(..., {patch: true})', function(assert) {
assert.expect(1);
var Model = Backbone.Model.extend({
url: '/test',
toJSON: function() {
assert.strictEqual(this.attributes.x, 1);
return _.clone(this.attributes);
}
});
var model = new Model({id: 1});
model.save({x: 1}, {patch: true});
});

QUnit.test('#2034 - nested set with silent only triggers one change', function(assert) {
assert.expect(1);
var model = new Backbone.Model();
Expand Down