我对骨干有点新意,有疑问。看起来当我的视图中的事件被触发时,我失去了“this”的上下文。如何保留此模型或获取模型的原始“this”。这是一个例子:
var MyListView = MyDBView.extend({
initialize: function(options){
},
render: function() {
//stuff here. I can access this.options here
},
dialogResponseYes: function(e){
//try to get this.options and options is undefined as this has changed to another element (a button)
}
});
那么,我如何得到这个的原始背景?
答案 0 :(得分:2)
如果你使用事件对象将事件绑定到你的视图,就像这里http://backbonejs.org/#View-delegateEvents一切都应该没问题,否则你很难用_.bindAll
initialize: function(options){
_.bindAll(this, "dialogResponseYes");
},
或
var MyListView = MyDBView.extend({
initialize: function(options){
},
events: {
'click div': 'dialogResponseYes' //example
},
render: function() {
},
dialogResponseYes: function(e){
}
});