Backbone events being fired without trigger
So I have a strange issue where my backbone events are getting fired even though they haven't been triggered yet. Essentially I'm making a note editor application. In the note itself, a user can press cmd + b to bold text, or any of the other normals. That then triggers an event which bubbles up to the AppController which should be subscribed to that event and call the correct method.
Here is the view for the note where the trigger is called:
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"
Then here is my AppController which binds to the event when the NoteView is instantiated
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
lastly, here is the @adminTools.boldSelection() function
boldSelection: ->
console.log( "yo" )
for some reason, upon page load, that console.log is being fired even though I never sent the trigger by pressing cmd + b in the note View. Anyone have any idea why a Backbone.Event would fire automatically?
This is a function call:
@adminTools.boldSelection()
#------------------------^^
This is a reference to a function:
@adminTools.boldSelection
You're supposed to hand on
a reference to a function so that it can call the function later. Your bindNoteEvents
should look more like this:
bindNoteEvents: (note) ->
note.on "boldSelection", @adminTools.boldSelection, note
note.on "italicizeSelection", @adminTools.italicizeSelection, note
note.on "underlineSelection", @adminTools.underlineSelection, note
# No parentheses here --------------------^^^^^^^^^^^^^^^^^^
链接地址: http://www.djcxy.com/p/63106.html
下一篇: 骨干事件被触发而没有触发