在事件中使用变量作为选择器
由于某种原因,我需要使用变量作为主干中事件的选择器,但我不知道如何执行此操作:
app.views.Selfcare = Backbone.View.extend({
events: {
click window.parent.document .close : "closeWindow"
},
closeWindow: function() {
//code
}
});
我必须使用不同的范围,并且我无法执行“click .close”:“closeWindow” 。
谢谢你的帮助。
for some reason I need to use a variable as the selector for events in backbone, but I can't figure how to do this :
app.views.Selfcare = Backbone.View.extend({
events: {
click window.parent.document .close : "closeWindow"
},
closeWindow: function() {
//code
}
});
I have to use a different scope and I can't do "click .close" : "closeWindow".
Thx for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我查看了 Backbone.js 的源代码,发现如果您的视图的
events
是一个函数,则调用该函数并将其返回值用作events
对象。这意味着您的代码可以这样更改:
THIS 是源代码中有趣的部分:
更新:
示例可在 JSFiddle 这里**
I had a look at Backbone.js's source code and found out that if your view's
events
is a function then the function is called and it's return value is used as theevents
object.This means that your code can be changed like this:
THIS is the interesting part of the source code:
Update:
Example is available on JSFiddle HERE**
我不确定您是否能够在那里使用变量。您可以使用内置事件方法(请参阅文档)添加自定义侦听器,然后将事件侦听器添加到
window.parent.document
以触发该自定义事件(使用 Events.trigger 方法)。也就是说,将此事件与 Backbone 完全分离(除非您不想这样做),然后沿着 addEventListener 路线走下去会容易得多:
我认为这应该可行,尽管我还没有测试过它(并且那里可能是一两个语法错误!)
I'm not sure that you'll be able to use a variable there. You could use the built-in Events methods (see documentation) to add a custom listener, then add an event listener to
window.parent.document
to trigger that custom event (use the Events.trigger method).That said, it would be much easier to decouple this event from Backbone entirely (unless you don't want to do that), and go down the addEventListener route:
I think that should work, although I haven't tested it (and there may be one or two syntactical errors!)