Model change event order in backbone js

2019-08-14 08:43发布

问题:

Is it guaranteed that 'change:property' events are always fired before 'change' events? Here is an example:

MyModel = Backbone.Model.extend({
    property1: 'value1',
    property2: 'value2'
});

var myModel = new MyModel();
myModel.bind('change:property1', function () { alert("change pty1"); })
       .bind('change', function () { alert("change"); })
       .bind('change:property2', function () { alert("change pty2"); });

Is it guaranteed that the function bound to 'change' will be fired last?

回答1:

Short answer: yes

Looking at the source code, yes the individual:changes are fired in the loop, and after that, if there was any change, the main change event will fire. None of these will fire if you passed silent: true.

The order of the individual change events firing depends on the order of the attributes passed to .set().