[英]Backbone.js View events are not triggering
I've been learning backbone.js and put up some code together finally. 我一直在学习骨干.js并最终将一些代码放在一起。 Everything is on a fiddle here .
这里的一切都在摆弄。
The problem precisely is, I think with the events
. 我认为问题恰恰与
events
。 They aren't triggering the view functions on click
. 它们不会在
click
上触发视图功能。
How to make it work? 如何使其运作? Am I missing any 'gotchas'?
我是否错过任何“陷阱”?
Views listen to events that emerge from the DOM element that it is bound to. 视图侦听从绑定到的DOM元素出现的事件。
You havent bound your view to any element. 您尚未将视图绑定到任何元素。 Which means that the view's element is an empty
div
that really isn't placed anywhere. 这意味着该视图的元素是一个空的
div
,实际上并未放置在任何地方。 So the events never reach the view. 因此,这些事件永远无法实现。 What you need to do is to bind the view to an element that hears the events coming from the buttons.
您需要做的是将视图绑定到一个听到来自按钮的事件的元素。
So set the view's el
so that it encompasses the buttons as well and voilà! 因此,设置视图的
el
使其包含按钮和外观吧!
You need to define the container of the view by setting the el
property, then it works as expected. 您需要通过设置
el
属性来定义视图的容器,然后按预期工作。
Backbone.View.extend({
el: "body",
events: {
"click #add-contact": "addContact",
"click #test": "alertMe"
},
Updated Fiddle: http://jsfiddle.net/fAB28/1/ 更新的小提琴: http : //jsfiddle.net/fAB28/1/
For extra credit and to make things more interesting :-) , you don't actually NEED to have a clearly defined el
for the Backbone.View to delegate your events properly. 为了获得额外的荣誉并使事情变得更加有趣:-),实际上您不需要为Backbone.View明确定义
el
即可正确地委派事件。 Take this for example: 以这个为例:
MyView = Backbone.View.extend({
// The events to delegate
events: {
'click #test':'onClick'
},
initialize: function() {
// delegateEvents() actually happens in constructor, before render()
// At this point there is no #test
},
render: function() {
// By default, el is an empty div
this.$el.html('<button id="test">testButton</button>');
return this;
},
onClick: function(event) {
console.log('clicked');
}
});
view = new MyView();
$('body').append(view.render().el); // We append this view to end of body
In the previous code, we didn't (explicitly) define an el
. 在前面的代码中,我们没有(明确地)定义
el
。 The most important thing to remember isn't that you have an explicit el
. 要记住的最重要的事情不是您拥有明确的
el
。 Rather, it's what @jakee says here: 而是@jakee在这里说的:
Views listen to events that emerge from the DOM element that it is bound to.
视图侦听从绑定到的DOM元素出现的事件。
The event hash you pass in or define inside a View are listened for within the view. 您在视图内传递或定义的事件哈希将在视图内监听。 Whether that is a defined
el
or the generic div, it listens for stuff inside it and since it delegates the event handlers, the handlers continue to point to this
view. 无论是已定义的
el
还是通用div,它都会侦听其中的内容,并且由于它委派了事件处理程序,因此处理程序继续指向this
视图。 Nice. 尼斯。
If you did something like this: 如果您执行以下操作:
$('#test').remove();
$('body').prepend('<button id="test">Second Test</button>');
We find that the #test is now outside of our view (the generic el that is appended at the end of our body. Clicking it does... nothing. But the coolest thing is this. 我们发现#test现在不在我们的视线范围内(附加在我们身体末端的通用el。单击它没有任何作用。但是最酷的是这个。
$('#test').remove();
$('div:last').html('<button id="test">Third Test</button>');
With this code we removed the #test at the top of the body and added it back into the view. 使用此代码,我们删除了主体顶部的#test并将其重新添加到视图中。 Clicking it, will resume it's
onClick()
functionality. 单击它,将恢复它的
onClick()
功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.