Fix recursion error when saving from within a change event.

This commit is contained in:
Matt
2010-11-22 20:49:33 -08:00
parent 9e1fcfad58
commit 57194beffa
2 changed files with 10 additions and 2 deletions

View File

@@ -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 File

@@ -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"});