【发布时间】:2012-07-22 22:36:58
【问题描述】:
当特定属性发生更改时,Backbone 模型触发自定义事件的好方法是什么?
到目前为止,这是我所拥有的最好的:
var model = Backbone.Model.extend({
initialize: function(){
// Bind the mode's "change" event to a custom function on itself called "customChanged"
this.on('change', this.customChanged);
},
// Custom function that fires when the "change" event fires
customChanged: function(){
// Fire this custom event if the specific attribute has been changed
if( this.hasChanged("a_specific_attribute") ){
this.trigger("change_the_specific_attribute");
}
}
})
谢谢!
【问题讨论】:
标签: javascript backbone.js backbone-events