gpt4 book ai didi

javascript - 主干 - 当特定属性更改时触发事件的模型

转载 作者:行者123 更新时间:2023-11-30 13:17:15 29 4
gpt4 key购买 nike

当特定属性发生变化时,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");
}
}
})

谢谢!

最佳答案

您已经可以绑定(bind)到特定于属性的更改事件:

var model = Backbone.Model.extend({
initialize: function () {
this.on("change:foo", this.onFooChanged);
},

onFooChanged: function () {
// "foo" property has changed.
}
});

关于javascript - 主干 - 当特定属性更改时触发事件的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11604543/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com