From 57194beffa3ca8c01f050cb9ddab8b38e45590bf Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 22 Nov 2010 20:49:33 -0800 Subject: [PATCH] Fix recursion error when saving from within a change event. --- backbone.js | 3 +-- test/model.js | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/backbone.js b/backbone.js index f8b9b0fa5..b39f371df 100644 --- a/backbone.js +++ b/backbone.js @@ -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; diff --git a/test/model.js b/test/model.js index 889498762..c103b98c2 100644 --- a/test/model.js +++ b/test/model.js @@ -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"});