Marionette.ItemView中的触发事件

时间:2013-03-21 16:15:56

标签: backbone.js marionette

这是我的ItemView的代码片段:

class List.GridRow extends Backbone.Marionette.ItemView
  tagName: 'tr'
  triggers:
    'click': 'row:clicked'

然后在我的复合视图中我这样做:

class List.GridView extends Backbone.Marionette.CompositeView
  template: 'mapping/list/templates/grid'
  itemView: List.GridRow
  itemViewContainer: 'tbody'

  initialize: (options) ->
    @listenTo @, 'itemview:row:clicked', (itemView, data) -> @rowClicked(itemView, data)

  rowClicked: (clickedItemView, data) =>

    # I need the original event information to check of the ctrl or shift key was pressed?
    #if !e.ctrlKey && !e.shiftKey

我在这里要做的是将原始事件信息传递给触发器处理程序,但我还没弄明白呢? Marionette有办法做到这一点吗?我错过了什么吗?

谢谢!

1 个答案:

答案 0 :(得分:7)

triggers的目的是为视图中的最小需求提供最小事件配置,同时还防止泄漏抽象。将原始事件args传递出视图会破坏视图的抽象,这应该是对事件args的控制。

如果您需要控制和班次键信息,则需要避免triggers配置并使用发布所需信息的标准事件。


class List.GridRow extends Backbone.Marionette.ItemView
  tagName: 'tr'
  events:
    'click': 'rowClicked'
  rowClicked (e) ->
    @trigger "row:clicked", 
      control: e.ctrlKey, 
      shift: e.shiftKey