【问题标题】:Backbone events being fired without trigger在没有触发器的情况下触发主干事件
【发布时间】:2013-06-01 02:01:56
【问题描述】:

所以我有一个奇怪的问题,即我的主干事件被触发,即使它们尚未被触发。本质上,我正在制作一个笔记编辑器应用程序。在注释本身中,用户可以按 cmd + b 来加粗文本,或任何其他法线。然后触发一个事件,该事件冒泡到应该订阅该事件并调用正确方法的 AppController。

这是调用触发器的注释的视图:

class MeetingNote.View.NoteView extends Backbone.View
    adminTemplate: _.template($('#AdminNoteTemplate').html())
    normalTemplate: _.template($('#NormalNoteTemplate').html())
    className: 'note' 
    events: 
            'keydown'                       : 'handleKeyDownsForStyling'

    # all of the normal backbone stuff.... init/render/blah

    handleKeyDownsForStyling: (e) ->
            if @admin == true 
                      if e.metaKey  
                            switch e.which 
                                  when 66 then @trigger "boldSelection" 
                                  when 73 then @trigger "italicizeSelection"
                                  when 85 then @trigger "underlineSelection" 

然后这是我的 AppController,它在 NoteView 实例化时绑定到事件

class MeetingNote.View.AppController extends Backbone.View
    template: _.template($('#MeetingNoteAppTemplate').html()) 
    className: 'MeetingNoteApp' 
    initialize: (options) -> 
            @admin = options.privilege                                                  
            @render()
    render: ->
            @$el.html(@template())
            $('#container').append(@$el)
            @initializeApp()

    initializeApp: ->                                       
            @adminTools = new MeetingNote.View.AdminTools if @admin == true
            notes = new MeetingNote.Collection.NotesCollection()
            notes.fetch { 
                    success: (collection) =>
                            _.each collection.models, (model) => 
                                  note = new MeetingNote.View.NoteView {model: model, privilege: @admin} 
                                  @bindNoteEvents note if @admin == true
            }

    bindNoteEvents: (note) ->
           note.on "boldSelection", @adminTools.boldSelection(), note
           note.on "italicizeSelection", @adminTools.italicizeSelection(), note
           note.on "underlineSelection", @adminTools.underlineSelection(), note

最后,这里是@adminTools.boldSelection() 函数

    boldSelection: ->
            console.log( "yo" )

由于某种原因,在页面加载时,即使我从未通过在注释视图中按 cmd + b 来发送触发器,也会触发 console.log。任何人都知道为什么 Backbone.Event 会自动触发?

【问题讨论】:

    标签: coffeescript backbone-events


    【解决方案1】:

    这是一个函数调用:

    @adminTools.boldSelection()
    #------------------------^^
    

    这是对函数的引用:

    @adminTools.boldSelection
    

    您应该向on 提供对函数的引用,以便稍后调用该函数。你的bindNoteEvents 应该看起来更像这样:

    bindNoteEvents: (note) ->
           note.on "boldSelection",      @adminTools.boldSelection,      note
           note.on "italicizeSelection", @adminTools.italicizeSelection, note
           note.on "underlineSelection", @adminTools.underlineSelection, note
           # No parentheses here --------------------^^^^^^^^^^^^^^^^^^
    

    【讨论】:

      猜你喜欢
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多