0

这张票没有帮助,那里的样本对我不起作用。我似乎无法让 jQuerydelegate正常工作。

是没有正确动态声明事件的 JSFiddle。我该怎么做呢?

小提琴中发生了什么?

动态事件的覆盖:

Backbone.View::addEvents = (events) ->
  @delegateEvents _.extend(_.clone(@events), events)  

一个聚合 DOM 选择器/事件的类,所以我们只声明它们的 HTML 对应项一次:

class Els
  constructor: () ->
    @signupButton = 'button#signup'
  events: () ->
    events = { }
    events['click ' + @signupButton] =  'clickSignup'
    events

处理 DOM 操作的类:

class UI extends Backbone.Model
  disableButton: () ->
    $(els.signupButton).addClass('disabled') 

木偶观点:

class MyRegion extends Backbone.Marionette.Region
  el: '#default-region'

class MyLayout extends Backbone.Marionette.Layout
  template: '#template-layout'
  regions:
    content: '#region-content'
  #events:
    #'click button#signup': 'clickSignup'
  initialize: (options) ->
    # This seems correct
    alert JSON.stringify(options.dynamicEvents)
    # FAIL!
    @addEvents options.dynamicEvents
  close: () ->
    @remove()
    @unbind()  
  clickSignup: (event) ->
    ui.disableButton()

应用启动:

els = new Els()
ui = new UI()

app = new Backbone.Marionette.Application()
app.start { }   

region = new MyRegion()

region.show new MyLayout(
  dynamicEvents: els.events()
)

如果我还可以在 中声明事件绑定,那就太好了Els,例如:

class Els
  constructor: () ->
    @signupButton = 'button#signup'
  events: () ->
    events = { }
    events['click ' + @signupButton] =  (event) ->
      ui.disableButton()
    events

我真的应该使用 EventEmitter,但这些示例假定所有内容都已实例化。

4

1 回答 1

0

这按预期工作

Backbone.View::addEvents = (events) ->
  @events = _.extend @events or {}, events
  @delegateEvents()
于 2012-12-04T19:24:46.020 回答