1

我目前正试图仅在悬停元素上触发Bootstrap弹出窗口。不幸的是,它触发了页面上的所有内容。下面是骨干脚本(在CoffeeScript中):在所有元素上触发的主干事件侦听器

Site.Views.Stories ||= {} 

class Site.Views.Stories.IndexView extends Backbone.View 
    template: JST["backbone/templates/stories/index"] 

    initialize:() -> 
    @options.stories.bind('reset', @addAll) 

    addAll:() => 
    @options.stories.each(@addOne) 

    addOne: (story) => 
    view = new Site.Views.Stories.StoryView({model : story}) 
    @$("#columns").append(view.render().el) 

    render: => 
    $(@el).html(@template(stories: @options.stories.toJSON())); 
    @addAll() 
    return this 

    events: => 
    "mouseover .rating" : this.showhover 

    showhover: => 
    this.$('.rating').popover('show'); 

回答

2

我猜,你有你的内部视图的el所以这种多.rating元素:

this.$('.rating').popover('show'); 

击中他们的所有。如果你只是想正在接收该事件的.rating然后说这样通过抓住特定.rating从事件处理程序的事件的说法:

showhover: (ev) => 
    $(ev.currentTarget).popover('show') 

几个其他的事情,而我在这里:

  1. 骨干不再自动设置@options,如果你想@options,那么你必须用手工做的initialize

    initialize: (@options) -> ... 
    
  2. 你并不需要使用的功能为您events,这将是罚款:

    events: 
        "mouseover .rating" : 'showhover' 
    
  3. 你不需要=>所有的方法。我认为你不需要为它们中的任何一个(假设你使用events对象而不是函数),但是你可以通过使用@listenTo或提供第三个参数到bind来解决这个问题。

相关问题