简体   繁体   English

所有收集项目的Backbonejs事件或每个ItemView的事件

[英]Backbonejs Events for all collection items or Events per ItemView

What approach would be more efficient? 哪种方法更有效?

I have a Backbone.Collection so i Create a Backbone.View to render this collection. 我有一个Backbone.Collection,所以我创建了一个Backbone.View来呈现此集合。 The CollectionView render method: CollectionView渲染方法:

render: ->
    container = document.createDocumentFragment()
    @collection.each (item) ->
        view = new ItemView(item)
        container.appendChild view.el
        view.render()
    $(el).append container

I can use the events in two forms. 我可以两种形式使用事件。

1.- Set the events object in CollectionView, so i need to declare the action of select an item in the CollectionView and "rescue" the model that i selected. 1.-在CollectionView中设置事件对象,因此我需要声明在CollectionView中选择一个项目的动作,然后“救援”我选择的模型。

CollectionView extends Backbone.View
    events:
        'click #itemView', 'onSelectItem'
    onSelectItem: ->
        ##Get the model
        ##Show ItemDetailView

2.- Set the events object per itemView, so the select method don't need to retrieve the model. 2.-为每个itemView设置事件对象,因此select方法不需要检索模型。

ItemView extends Backbone.View
    events:
        'click #div','onSelect'
    onSelect: ->
        #Show ItemDetailView

Which of this options are better? 以下哪个选项更好?

EDITED: I create a JSperf snippet http://jsperf.com/backbone-events-on-collectionview-or-per-itemview 编辑:我创建了一个JSperf代码段http://jsperf.com/backbone-events-on-collectionview-or-per-itemview

JSperf show us that the ItemView approach is more faster, but is this the only metric of importance? JSperf向我们展示了ItemView方法更快,但这是唯一的重要指标吗?

If I understand you correctly, option 2 seems the most sane. 如果我理解正确,选项2似乎是最理智的。 You will be referring to this.model as opposed to digging through the collection to find the model associated with the clicked view. 您将引用this.model ,而不是浏览集合以查找与单击的视图关联的模型。

The second approach is much better. 第二种方法要好得多。 Performance aside, the code is much more straightforward. 除了性能之外,该代码更加简单明了。 In 6 months, if you had to return to this code, would you think the event would be handled in the ItemView, or the CollectionView? 在6个月内,如果您必须返回此代码,您是否认为该事件将在ItemView或CollectionView中处理? It is a click event on the ItemView, so that's where I would go to look for how it is handled. 这是ItemView上的click事件,因此我将在那里查找其处理方式。

Is there a good reason to handle the event in the collection? 是否有充分的理由来处理收藏中的事件? If you needed the handle the event from the CollectionView, you could delegate the event to the collection. 如果需要从CollectionView处理事件,则可以将事件委托给集合。 A little redirection, but, to me, this is far clearer. 稍微重定向一下,但是对我来说,这更清楚了。 For example: 例如:

# The collection view

initialize: ->
  @listenTo @, 'selected', @itemSelected

render: ->
  container = document.createDocumentFragment()
  @collection.each (item) ->
    view = new ItemView(item, parent: @)
    container.appendChild view.el
    view.render()
  $(el).append container

itemSelected: (model)->
  # Do whatever you need to here, like 
  # show the ItemDetailView in the container

# The ItemView

ItemView extends Backbone.View
  events:
    'click #div','onSelect'
  onSelect: ->
    @options.parent.trigger('selected', @model, @)
    #Show ItemDetailView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM