覆盖骨干“设定”的方法 [英] Override backbone 'set' method
本文介绍了覆盖骨干“设定”的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要重写骨干一套方法,以便每当我一值设置为骨干示范该属性登记被调用回调,不检查该属性的同一previous值。
VAR模型= Backbone.Model.extend({
默认值:{
为prop1:真
}
});VAR视图= Backbone.View.extend({
初始化:功能(){
this.listenTo(this.model,改变:为prop1,this.callback); },
回调:函数(){
//集合呼吁为prop1
}
});VAR M1 =新模式();
VAR V1 =新的视图({型号:M1});
m1.set(为prop1,真正的); //它不会触发回调,因为我设置相同的值,为prop1
解决方案
您可以写这样设置骨干模型的新方法:
VAR模型= Backbone.Model.extend({
默认值:{
为prop1:真实的;
}, //重载集
设置:功能(属性,选项){
//将被触发每当一套被称为
如果(attributes.hasOwnProperty(为prop1)){
this.trigger('的变化:为prop1');
} 返回Backbone.Model.prototype.set.call(此,属性选项);
}
});
I want to override backbone set method so that whenever I set a value to backbone Model the callbacks registered on that attribute get called without checking for same previous value of that attribute .
var model = Backbone.Model.extend({
defaults : {
prop1 : true
}
});
var view = Backbone.View.extend({
initialize : function(){
this.listenTo(this.model,"change:prop1", this.callback);
},
callback : function(){
// set is called on prop1
}
});
var m1 = new model();
var v1 = new view({model:m1});
m1.set("prop1",true); // It doesn't trigger callback because I'm setting the same value to prop1
解决方案
You can write a new method in backbone model set like this :
var model = Backbone.Model.extend({
defaults: {
prop1: true;
},
// Overriding set
set: function(attributes, options) {
// Will be triggered whenever set is called
if (attributes.hasOwnProperty(prop1)) {
this.trigger('change:prop1');
}
return Backbone.Model.prototype.set.call(this, attributes, options);
}
});
这篇关于覆盖骨干“设定”的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文