木偶CompositeView中的自己呈现在收集,而不是ItemView控件(木偶Rails)的每个模型 [英] Marionette CompositeView renders itself for each Model in Collection instead of ItemView (Marionette Rails)

查看:89
本文介绍了木偶CompositeView中的自己呈现在收集,而不是ItemView控件(木偶Rails)的每个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我试图呈现一个CompositeView中的同一个表头,其中一个集合中的每个模型渲染成一个,并追加到一个简单的四列的列表。我下面的德里克的的例子,只有一点点的变化,但不幸的是已经得到了一些pretty奇怪的结果。

Basically, I'm trying to render a CompositeView as a simple four-column list with a table header, where each model in a collection is rendered into a and appended to the . I'm following an example of Derick's quite closely with only a little variation, but unfortunately have gotten some pretty strange results.

重新呈现集合中的每个项目,产生了新的表和表头。在此之前,它被渲染CompositeView中本身。

Instead of rendering each itemView, the view instead references itself and re-renders for each item in the collection, producing a new table and table head. Before that, It was rendering the compositeView in itself.

我有一个ItemView控件,这是一组项目的模板,引用它CompositeView中这是一个表

I have an itemView, the template of which is a group of items, and a compositeView that references it which is a table

该CompositeView中的:

The CompositeView:

class App.module('Views.Parts').Index extends Backbone.Marionette.CompositeView
   template: 'parts/index'
   itemView: App.Views.Parts.Part
   tagName: 'table'
   itemViewContainer: 'tbody'
   appendHtml: (collectionView, itemView, index)->
           collectionView.$el.append(itemView.el)

该ItemView控件:

The ItemView:

class App.module('Views.Parts').Part extends Backbone.App.ItemView
       tagName: 'tr'
       template: 'parts/part'
       events:
               "click .destroy": "destroy"
       destroy: (e) ->
               e.preventDefault()
               @model.destroy()
       onRender: ->
               @stickIt()

控制器

class App.Controllers.Parts
       constructor: ->
               @parts = new App.Collections.Parts
               @parts.reset(App.parts)
               App.parts = null

       showView: (view)->
               App.mainRegion.show view

       index: ->
               view = new App.Views.Parts.Index
                       collection: @parts
               @showView view

我还听说,宣布CompositeView中之前ItemView控件是必要的 - 但是,因为它是一个木偶Rails项目,该意见实际上是生活在不同的目录中。我必须声明它们的顺序或将它们以另一种方式相应地相互结合?

I have also heard that declaring an ItemView before the CompositeView is necessary--however since it is a Marionette Rails project, the views are actually living in different directories. Would I have to declare their order or bind them to each other accordingly in another way?

推荐答案

TL; DR Marionette.CompositeView 的使用本身作为项目视图类型,如果没有定义。坐落于原型ItemView控件属性,使其使用正确的项目视图

TL;DR Marionette.CompositeView uses itself as the item view type if none defined. Set the itemView property on prototype to make it use the correct item view

在我们的例子中的问题是木偶由 ItemView控件的一个的 CompositeView中的的href=\"https://github.com/marionettejs/backbone.marionette/blob/3761df5c72a401f71b43d9653b91efc6552497b9/lib/backbone.marionette.js#L1770\"相对=nofollow>以下逻辑:

In our case issue was that Marionette gets the ItemView of a CompositeView by the following logic:

getItemView: function(item){
  var itemView = Marionette.getOption(this, "itemView") || this.constructor;

  if (!itemView){
    throwError("An `itemView` must be specified", "NoItemViewError");
  }

  return itemView;
}

我们已经定义的 CompositeView中的的是:

class sm.Views.GreetingPicker extends Marionette.CompositeView
  template: 'greeting_picker'
  el: '.message-choice'
  itemView: sm.Views.WelcomeMessage

当的 CompositeView中的 ItemView控件的&安培;的布局的是他们捆绑在一起是在一个文件中(通过使用Rails的链轮服务),该文件中加载一下子也没有问题。

When the CompositeView, ItemView & Layout that tied them together were in one file (served by Rails using Sprockets), the file loaded all at once and there was no problem.

在我分裂了的布局 CompositeView中的的&安培;的 ItemView控件的,在 CompositeView中的的的appendHtml的两个参数是同一类型的CompositeView中。这是通过在开头提到的木偶的逻辑引起的。

After I split up the Layout, CompositeView & ItemView, the CompositeView's appendHtml's both arguments were of the same type as CompositeView. This was caused by the Marionette's logic mentioned in the beginning.

在手动设置构造的 ItemView控件的属性初始化。像这样的:

In the initializer set the itemView property on constructor manually. Like this:

class sm.Views.GreetingPicker extends Marionette.CompositeView
  initialize: ->
    @constructor::['itemView'] = sm.Views.WelcomeMessage

这篇关于木偶CompositeView中的自己呈现在收集,而不是ItemView控件(木偶Rails)的每个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆