事件绑定点击与backbone.js & jQuery
我需要在 backbone.js 视图中绑定两个事件才能切换菜单。这个想法是,如果单击 ID 为 #toggler
的按钮,则会出现菜单,并且在 #menu
元素之外的任何单击都会隐藏菜单。
不幸的是,如果没有在每次点击时调用 outsideMenuHandler()
,无论我是否单击了 #menu
元素,我都无法使用主干的事件绑定来实现此功能。
我应该改变什么才能使这项工作正常进行?
这是我在backbone.js中所做的,它没有按预期工作:
myView = Backbone.View.extend({
events: {
'click #menu': 'insideMenuHandler',
'click': 'outsideMenuHandler'
}
insideMenuHandler: function(e) {}, // Called when clicking #menu
outsideMenuHandler: function(e) {} // Called on every click both on and off #menu
}
作为参考,以下是我单独使用 jQuery 所做的事情:
$(document).click(function(event) {
if ( $(event.target).closest('#menu').get(0) == null ) {
$("#menu").slideUp();
}
});
I need to bind two events in my backbone.js
-view in order to toggle a menu. The idea is that if a button with the id #toggler
is clicked the menu appears and any click outside the #menu
element will hide the menu.
Unfortunately I cannot get this working with backbone's event binding without having the outsideMenuHandler()
called on every click regardless if I clicked on the #menu
element, or not.
What should I change to make this work?
This is what I have done in backbone.js, which doesn't work as intended:
myView = Backbone.View.extend({
events: {
'click #menu': 'insideMenuHandler',
'click': 'outsideMenuHandler'
}
insideMenuHandler: function(e) {}, // Called when clicking #menu
outsideMenuHandler: function(e) {} // Called on every click both on and off #menu
}
Just as a reference, here's what I would do using jQuery alone:
$(document).click(function(event) {
if ( $(event.target).closest('#menu').get(0) == null ) {
$("#menu").slideUp();
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几件事您需要解决。
首先,如果您的
insideMenuHandler
返回false
或调用e.stopPropogation()
那么您的outsideMenuHandler
将不会点击#menu
时会收到呼叫。例如:但这不是您的全部问题。您的
outsideMenuHandler
只会在您的视图上点击时被调用;因此,如果有人点击您视图之外的页面,您的outsideMenuHandler
将不会被调用,您的菜单也不会关闭。如果您希望当有人点击#menu
之外的任何位置时菜单会关闭,那么您必须手动绑定到body
并在视图被销毁时手动取消绑定。像这样:然后确保正确删除您的视图。例如:
There are a couple things you need to sort out.
First of all, if your
insideMenuHandler
returnsfalse
or callse.stopPropogation()
then youroutsideMenuHandler
won't get called for clicks on#menu
. For example:But that's not your whole problem. Your
outsideMenuHandler
will only be called for clicks on your view; so, if someone clicks on the page outside your view, youroutsideMenuHandler
won't get called and your menu won't go down. If you want the menu to go down when someone clicks anywhere outside#menu
, then you'll have to manually bind tobody
and manually unbind when your view is destroyed. Something like this:and then be sure to properly remove your view. For example:
问题是,您将事件绑定到定义为视图 el 的 DOM 元素。因此,如果这不是 jquery 示例中的
window.document
,您将无法注意到它们之外的任何点击。在您的示例中,最简单的方法是使用 jquery 手动添加事件,而不是使用主干。The problem is, that you bind your event to the DOM element that you define as the views el. So if this is not the
window.document
as in your jquery example you cant notice any click outside them. The easiest way in your example would be to add the events manually with jquery and not with backbone.