如何引用在backbone.js视图中定义的事件的事件上下文?
我的视图设置了一些事件,并且我想引用,例如,被单击的按钮元素,因为它具有我需要的数据属性。我想这样做:
events: {
'click #testGadget': 'fireEvent',
...
},
fireEvent: function(){
var x = $(this).data('iCanHaz')
}
但是“this”变量的范围仅限于视图本身。我知道有一种方法可以完成我想要做的事情,但我似乎无法以返回任何谷歌点击的方式表达我的问题。
I have my view set up with some events and I want to reference, for example, the button element that was clicked because it has a data attribute I need. I want to do it like so:
events: {
'click #testGadget': 'fireEvent',
...
},
fireEvent: function(){
var x = $(this).data('iCanHaz')
}
But the 'this' variable is scoped to the view itself. I know there's a way to accomplish what I'm looking to do but I can't seem to word my question in a way that returns any google hits.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以简单地使用
event.target
属性来完成:请参阅 Event对象文档
Can simply be done with the
event.target
property:see Event Object doc