我正在尝试在另一个对象中调用我的视图的“attachmentClicked”函数时进行监听。这是最初调用事件的类:
class AttachmentView extends AttachmentViewerView
template: _.template($('#AttachmentViewTemplate').html())
className: "attachmentView"
#
# initialize ->
#
initialize: ->
console.log "AttachmentView initialized"
@render()
events: {
'click' : 'attachmentClicked'
'dblclick' : 'openAttachment'
}
#
# render ->
#
render: ->
@$el.html(@template(@model.toJSON()))
$('div.attachmentViewerView').append(@el)
# @bind 'event', method
#
# attachmentClicked ->
#
attachmentClicked: ->
@$el.addClass('selectedAttachmentView')
该对象在单击时调用attachmentClicked
,现在在另一个创建该对象的类中,我正在尝试侦听该事件。这是那堂课
class AttachmentViewerView extends AttachmentAppController
template: _.template($('#AttachmentViewerTemplate').html())
className: "attachmentViewerView"
#
# initialize ->
#
initialize: (options) ->
console.log "AttachmentViewer initialized"
@office = options.office
@ticket = options.ticket
@attachmentViews = []
@render()
#
# render ->
#
render: ->
@$el.html(@template())
# Append to fileViewer Div
$('#attachmentViewerWindow').append(@el)
@renderFiles()
#
# bindEvents ->
#
bindEvents: (view) ->
@listenTo view, 'attachmentClicked', @attachmentClicked
#
# renderFiles ->
#
renderFiles: ->
@attachments = new AttachmentCollection({@office, @ticket})
@attachments.fetch({
success: (collection) =>
_.each collection.models, (model) =>
# Create the attachment views and bind events right away
@bindEvents new AttachmentView({model: model})
})
#
# attachmentClicked ->
#
attachmentClicked: (attachment) ->
console.log( @ )
@$el.find('.selectedAttachmentView').removeClass('selectedAttachmentView') unless @selected == attachment
@selected = attachment
所以发生的事情是,当这个类被创建时,它最终调用 renderFiles 从服务器获取文件,然后它为每个返回的模型创建一个视图,并以它作为参数调用 bindEvent。
然后 bindEvent 尝试监听新创建的项目的 attachmentClicked 方法并将其绑定到此类的 attachmentClicked 函数。但是,它不起作用。我尝试了几种方法,但不确定我的问题出在哪里。指导将不胜感激。