Backbone not binding events to jQuery Popover

给你一囗甜甜゛ 提交于 2019-12-12 10:11:38

问题


This question seems similar to those asked in a few other posts: here and here. I feel like I understand the concept, but I'm still having issues.

I am using the Twitter Bootstrap stuff for javascript popovers; the popover is where I want to bind events. The Bootstrap javascript inserts/removes the html each time you call .popover('show')/.popover('hide'). The events that are bound to the html inside of the popover do not get called. From what I read, Backbone uses jQuery.delegate() so it shouldn't matter if the html exists, but something is not working correctly

events:
    "click"             : "popover"
    "click .close"      : "close_popover"

Of these events, the first click event works but not the second (which is inside the popover).

popover: ->
  @el.popover('show')
  @delegateEvents(@events) #added from link

close_popover: ->
  @el.popover('hide')

Thanks.

Working on a jsFiddle that duplicates the problem. Added the code from the suggested link--still doesn't work.


回答1:


Your code looks fine. Can you confirm that an element with class close exists as a child of your view's el, and that it's what you're actually clicking on? (Try right-clicking on the element and inspecting it with Chrome Developer Tools or Firebug).




回答2:


You may be missing some indentations that are crucial when writing CoffeeScript:

Instead of

events:
"click"             : "popover" 
"click .close"      : "close_popover"

You want to indent the events

events:
    "click"        : "popover"
    "click .close" : "close_popover"

Without that indentation, the events become properties of the object, not of the "events" property and they will never be registered.



来源:https://stackoverflow.com/questions/7983636/backbone-not-binding-events-to-jquery-popover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!