0
votes

My backbone.js model has an array property. I bound the change event to save().

After sync() (triggered by save(), my app server returns an identical JSON, but backbone thinks the array has been changed (due to a different reference to the array I guess?), and trigger changes again. Then an infinite loop occurs.

save() -> sync() -> triggered `change` -> save()...

What shall I do?

Idea: I can bind the change event to a function that checks if the changed attributes are of type object/array, and do a deep comparison and call save only if the array/object really changed. If true then save()?

Thanks!

2

2 Answers

1
votes

Try the Edge version of Backbone (master branch) this behavior changed after 0.9.9 - see https://github.com/documentcloud/backbone/pull/2004

0
votes

Backbone has a special option on many methods to prevent just this sort of issue: silent:true. If you pass that option to your save method, the resulting sync won't trigger a change event.

So, if you want to set your change event handler to save silently, something like:

changeHandler: function() {
    this.save({silent:true});
}

should do the trick.