简体   繁体   English

主干事件委托 eventName 问题

[英]Backbone Event Delegation eventName issue

I have a view with a simple enough events object (in coffeescript because it's sweet)我有一个足够简单的事件对象的视图(在咖啡脚本中,因为它很甜)

APP.bb.Recipe_Index = Backbone.View.extend

    template: JST['templates/Recipe_Index']

    el: $ "#recipes"

    events:
        'click a' : 'testFunction'

    testFunction: () ->
        console.log 'test called'
        return 'this function'

My events aren't bound.我的活动不受约束。 When I get to line 967 in the unminified 0.5.3 Backbone.js file当我到达未缩小的 0.5.3 Backbone.js 文件中的第 967 行时

delegateEvents : function(events) {
  //snip
  for (var key in events) {
    var method = this[events[key]];
    if (!method) throw new Error('Event "' + events[key] + '" does not exist');
    var match = key.match(eventSplitter);
    var eventName = match[1], selector = match[2];
    method = _.bind(method, this);
    eventName += '.delegateEvents' + this.cid;
    if (selector === '') {
      $(this.el).bind(eventName, method);
    } else {
      $(this.el).delegate(selector, eventName, method);  <-- THIS ONE
    }
  }
},

It ends up with incorrect jQuery syntax when I have a breakpoint set in Chrome:当我在 Chrome 中设置断点时,它以不正确的 jQuery 语法结束:

selector == 'a'
eventName == click.delegateEventsview8'
method == 'function() {[native code]}'

The selector is correct, but I'm not sure why on line 963 there is an appendation to my 'click' string which indicates the event type.选择器是正确的,但我不知道为什么在第 963 行有一个附加到我的“click”字符串,它指示事件类型。 The method reads out as my console.log method before it's bound with underscore, so that's correct.该方法在与下划线绑定之前读取为我的console.log方法,所以这是正确的。

The end result is that my events aren't triggered.最终结果是我的事件没有被触发。 Wondering what I've hooked up wrong here.想知道我在这里连接错了什么。

The event name click.delegateEventsview8 is actually a correct value for a jQuery event.事件名称click.delegateEventsview8实际上是 jQuery 事件的正确值。 Backbone is using the namespaced events functionality of jQuery. Backbone 正在使用 jQuery 的命名空间事件功能。 ( You can read more about it here ) 你可以在这里阅读更多关于它的信息

Backbone is using namespaced events so it can easily unbind all the events it added in its undelegateEvents function. Backbone 使用命名空间事件,因此它可以轻松解除绑定在其undelegateEvents函数中添加的所有事件。 By using a namespace, Backbone can be sure it only unbinds the events it added.通过使用命名空间,Backbone 可以确保它只解除它添加的事件的绑定。 That way it won't unbind any events that it didn't add.这样它就不会解绑它没有添加的任何事件。

undelegateEvents : function() {
  $(this.el).unbind('.delegateEvents' + this.cid);
},

As for why the event isn't triggered, I don't know.至于为什么没有触发事件,我不知道。

Try manually adding a click event in your render method after the template has been rendered to the view's el and see if it's called then.在模板被渲染到视图的el之后,尝试在你的渲染方法中手动添加一个点击事件,然后看看它是否被调用。 If not, then you know something is wrong with the selector.如果没有,那么您就知道选择器有问题。

render: function() {
  $(this.el).append(//your template code)
  this.$('a').click(this.testFunction);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM