我有这样的主干 View
window.InputView = Backbone.View.extend({
tagName:'input',
className:'',
attributes:{},
initialize:function(){
this.attributes=this.model.attributes;
this.el = this.make(this.tagName,this.attributes,'');
}
});
我遇到的问题是,当我修改 View 的 attributes
散列时,它不会反射(reflect)在 el
上,
所以我必须做这样的事情 this.el = this.make(this.tagName,this.attributes,'');
为反射(reflect)变化。
这是唯一的方法,还是有更好的方法?喜欢自动化吗?
最佳答案
要在模型更改时自动执行您尝试执行的操作,您需要将方法绑定(bind)到模型的更改事件。在您的初始化方法中,您需要类似以下内容:
initialize: function() {
this.model.on("change", updateElement);
...
}
然后稍后在您的 View 中定义该方法:
updateElement: function() {
//use this.model.attributes to update your el
}
现在,只要与该 View 关联的模型发生更改,updateElement
方法就会运行。
关于javascript - Backbone.js : Changing view. 属性没有反射(reflect)在 view.el 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9554628/