我有一个表格,当我单击一行时,我会在侧面 div 中显示详细信息。我在行 (tr) 上创建了一个事件,每次单击该行时都会触发该事件。我遇到的问题是该事件被多次触发。就好像每次触发事件时都在创建一个新视图,但没有创建一个新视图。这是代码:
GroupView = class App.GroupView extends Backbone.View
template: App.Utils.getTemplate 'group'
selectedId: 0
events:
'click tr': 'selectGroup'
selectGroup: (e) ->
thisEl = $(e.currentTarget)
$(thisEl).closest('tr').siblings().removeClass 'selected'
$(thisEl).closest('tr').addClass 'selected'
@selectedId = $(thisEl).data().id
@getGroupDetails @selectedId
render: ->
$(@$el).html @template
groups: @collection.models
if @selectedId is 0
firstRowEl = $(@$el).find('tr:first')
$(firstRowEl).addClass 'selected'
@selectedId = $(firstRowEl).data().id
@getGroupDetails @selectedId
getGroupDetails: (id, platform) ->
$('<img/>',
src: 'img/ajax_loader_2.gif'
class: 'ajax-loader'
).appendTo '.group-details'
renderGroupDetails id
groupDetails = new GroupDetailsView
el: '.group-details'
model: groupDetailsModel
renderGroupDetails = (id)->
groupDetails.el = '.group-details'
groupDetails.stopListening groupDetailsModel
groupDetails.listenTo groupDetailsModel, 'change', ->
groupDetails.model =
groupDetails: groupDetailsModel.toJSON()
groupDetails.render()
groupDetailsModel.fetch
data:
id: id
processData: true
问题是,当我使用 时groupDetails.stopListening groupDetailsModel
,多事件触发问题已解决,但groupDetails
视图中没有任何事件被触发。
任何帮助表示赞赏。
干杯,
基亚诺什