Permalink
Browse files

Fix recursion error when saving from within a change event.

  • Loading branch information...
1 parent 9e1fcfa commit 57194beffa3ca8c01f050cb9ddab8b38e45590bf Matt committed Nov 23, 2010
Showing with 10 additions and 2 deletions.
  1. +1 −2 backbone.js
  2. +9 −0 test/model.js
View
@@ -242,9 +242,8 @@
// If the server returns an attributes hash that differs, the model's
// state will be `set` again.
save : function(attrs, options) {
- attrs || (attrs = {});
options || (options = {});
- if (!this.set(attrs, options)) return false;
+ if (attrs && !this.set(attrs, options)) return false;
var model = this;
var success = function(resp) {
if (!model.set(model.parse(resp), options)) return false;
View
@@ -151,6 +151,15 @@ $(document).ready(function() {
model.change();
equals(model.get('name'), 'Rob');
});
+
+ test("Model: save within change event", function () {
+ var model = new Backbone.Model({firstName : "Taylor", lastName: "Swift"});
+ model.bind('change', function () {
+ model.save();
+ ok(_.isEqual(lastRequest[1], model));
+ });
+ model.set({lastName: 'Hicks'});
+ });
test("Model: save", function() {
doc.save({title : "Henry V"});

2 comments on commit 57194be

Contributor

KrisJordan replied Feb 6, 2011

This commit seems to cause other scenarios to regress. Specifically, because set calls validate and set is getting short wired when attrs is false, validate never runs on saves to Models instantiated with attributes. Because Collection.create's implementation relies on that scenario validation does not get triggered when Models are created using Collection.create even though the documentation implies otherwise (http://documentcloud.github.com/backbone/#Collection-create).

Examples here: https://gist.github.com/813138

On further inspection of set it now shortwire returns this with empty attrs before running validate so getting validate to run on Collection.create may just need a localized fix there.

Owner

jashkenas replied Feb 6, 2011

Thanks for the fix in your pull request for Collection#create ... which has been merged to master.

Please sign in to comment.