Backbone.js的事件绑定 [英] Backbone.js Event Binding

查看:104
本文介绍了Backbone.js的事件绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Backbone.js的对每个型号的观点分段控制型的UI元素。它们分别由具有几里元素的UL认证。我要绑定,使得当被点击这些元件中的一个,我可以确定一个被点击它,并更新与适当的值的模型的事件

I'm using Backbone.js have a segmented control-type UI element for each model's view. They are each made up of a ul with a few li elements. I want to bind an event such that when one of these elements is clicked, I can determine which one has been clicked and update the model with the appropriate value.

的问题是,骨干结合事件(这些是在视图中的事件的散列),例如,这在回调函数指的是视图,而不是利元素。这意味着,我不能确定哪几个里的元素已被点击。如果我用一个正常的jQuery的结合,我可以有本绑定到李的元素,但我没有跟踪模式了,所以我不能更新它。

The problem is that Backbone binds the events (these are in the events hash of the view) such that "this" in the callback function refers to the view, not the li elements. This means that I can not determine which of the several li elements has been clicked. If I used a normal jQuery binding, I can have "this" bound to the li elements, but then I don't have track of the model anymore, so I can't update it.

推荐答案

设置这个来无论发生什么事是在时间方便的jQuery的习惯是pretty讨厌的模式,在我看来 - 幸运的是,你永远不必依靠它:

jQuery's habit of setting this to whatever happens to be convenient at the time is a pretty nasty pattern, in my opinion -- fortunately, you never have to rely on it:

onClick: function(e) {
  this;                // Still the view instance (as it should be).
  e.target;            // The element that was clicked.
  e.currentTarget;     // The element that was bound by the click event.
}

...您可以使用目标 currentTarget当前事件对象的,适当的。

... You can use the target or currentTarget of the event object, as appropriate.

这篇关于Backbone.js的事件绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆