Backbone.js 事件激发时el未定义
我从一个视图触发一个事件,如下所示:Backbone.js 事件激发时el未定义,backbone.js,backbone-events,backbone-views,Backbone.js,Backbone Events,Backbone Views,我从一个视图触发一个事件,如下所示: select: function () { // Shorthand for the application namespace var app = brickpile.app; // Trigger the selected event app.trigger('selected', this.model); } 并绑定到另一个视图中的同一事件: initialize: function () { // Short
select: function () {
// Shorthand for the application namespace
var app = brickpile.app;
// Trigger the selected event
app.trigger('selected', this.model);
}
并绑定到另一个视图中的同一事件:
initialize: function () {
// Shorthand for the application namespace
var app = brickpile.app;
// bind to the selected event
app.bind('selected', this.selected);
},
在我的函数中,我得到了当前实例的el属性
selected: function (model) {
// find the input hidden located in this views el
$(this.el)... // is undefined
},
我错过了什么?我会引用这句话来回答你的问题
绑定“此”
也许最常见的JavaScript“gotcha”是这样一个事实:当您将函数作为回调传递时,其
此项的值已丢失。在处理事件和
回调时,您通常会发现依赖u.bind和u.bindAll非常有用
来自下划线.js
将回调绑定到主干事件时,可以选择传递
可选的第三个参数,用于指定
稍后将调用回调
试一试
或
可能是范围问题,请尝试app.bind('selected',this.selected,this)@正确,谢谢你的帮助!您也可以在
初始化中.bindAll(这是“选定的”)
,而不必担心bind
@nikoshr的上下文参数-请提供答案作为答案,而不是comments@DerickBailey在电话屏幕上用法语在虚拟键盘上输入完整的答案可能很棘手:)这是我的答案
app.bind('selected', this.selected, this);
_.bindAll(this, 'selected');
app.bind('selected', this.selected);