documentcloud/backbone — Issue #959: Change event produce an infinite loop
Author: mistakster
Created: 2012-02-05 07:36:37+00:00
State: open

In the last version (0.9.1) I have faced defect which produce an infinite loop.

$(function () {

// define model class
var AppModel = Backbone.Model.extend({});

// define view class
var AppView = Backbone.View.extend({
initialize: function () {
_.bindAll(this, "render");
$(this.el).appendTo("body");
this.render();
},
render: function () {
console.log("render");
this.model.set({c: 1}, {silent: true});
$(this.el).html('

' + this.model.get("a") + '

' + this.model.get("b") + '

');
return this;
}
});

// create model instance
var model = new AppModel({a: 10, b: 20});

// create view for model instance
var view = new AppView({model: model});

// any changes in the model run redraw
model.bind("change", function () {
view.render();
});

// generate random data
$(".btn-update").on("click", function () {
model.set({
a: Math.round(100 * Math.random()),
b: Math.round(100 * Math.random())
});
});

});

Any change in model begins redraw view. But I need to set value of one of properties of model in `render` function.
I set an option {silent: true} to suppress additional event. It perfectly worked in version 0.5.3, but has broken in 0.9.1.

It is defect in design of the application or an error in Backbone?