骨干动态创建的“厄尔尼诺”不具约束力的事件 [英] Backbone Dynamically created 'el' not binding events

查看:136
本文介绍了骨干动态创建的“厄尔尼诺”不具约束力的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像其他许多用户在这里我有一个问题,'厄尔尼诺'和事件。在我的情况我已经测试了多种解决方案,包括使用默认的EL(只​​是'')设置记名,使用jQuery选择设置EL一个选择器和wainting的DOM做好准备。这些解决方案已经在结合事件和所有,但默认的E1和标签名在生成HTML失败失败。
有关code
查看

Like many other users out here I have a problem with 'el' and events. In my situation I have tested multiple solutions including, using the default el (just '') setting tagName, setting el to a selector using a jQuery selector and wainting for the DOM to be ready. Each of these solutions have failed at binding the events and all but the default el and tagName have failed at generating html. Relevant code View

$ ->
   class Aggregator.Views.Streams.StreamView extends Backbone.View
      template:JST["backbone/templates/stream"]
      el: $('.item')
      events:
       "click div" : "testing"
      initialize: (model)->
       console.log @el
       _.bindAll this , 'render'
       @model.bind 'change', @render
       @model.view = @
       @render()
       @delegateEvents()
      render: ->
       $(@el).html  "test"
       console.log @el
       @
      testing: ->
       alert "EVENT"
       #@model.clear()

函数来调用视图(从另一个视图中提取)

Function that calls view (extracted from another view)

view = new Aggregator.Views.Streams.StreamView({model: stream})
    console.log view.el
    $(@el).append view.render().el

我有点糊涂了,我已经设置EL,但它要么不建立或者不绑定的事件。我试着等待DOM加载和EL传递到构造函数,但没有成功。对我最有可能明显的错误任何帮助将是AP preciated。

I'm a bit confused, I have set el but it either doesn't create or it doesn't bind events. I've tried waiting for the DOM to load and passing el to the constructor but no success. Any help on my most likely obvious mistake would be appreciated.

推荐答案

对不起,我不舒服的CoffeeScript,但我认为我认识您的活动散列

Sorry i'm not comfortable with coffeescript, though i think i recognised your event hash

events: {
  "click div" : "testing"
}

和我敢打赌,你试图绑定你一般视图元素在这个测试功能,点击?
有问题。

and i bet you are trying to bind this testing function to the click on your general view element? there is the problem.

你的点击事件实际上没有(选择明智)是:

what your click event actually does (selector-wise) is:

$('div', viewElement).click(fn);

意思,你不针对viewElement格但div的INSIDE您的视图元素。

meaning, you don't target the viewElement div but div's INSIDE your view element.

如果你希望绑定到的普遍看法元素本身,
没有选择自定义事件

if you want to bind to the general view element itself, define an event without a selector

在您的情况:

events: {
  "click" : "testing"
}

这篇关于骨干动态创建的“厄尔尼诺”不具约束力的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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