Marionette CompositeView行为

时间:2015-08-31 06:30:10

标签: backbone.js marionette

我正在尝试按照Backbone Rails教程进行操作,并且试图在CompositeView中渲染我的链接集合时遇到困难,不涉及嵌套。我怀疑教程已经过时,但由于我还缺乏Backbone技能,我无法指出问题。请看下面的代码:

创建导航链接集合。

@TestApp.module "Entities", (Entities, App, Backbone, Marionette, $, _) ->

class Entities.Navigation extends Backbone.Model

class Entities.NavigationCollection extends Backbone.Collection
    model: Entities.Navigation

API = 
    getLinks: ->
        new Entities.NavigationCollection [
            { name: "one"   }
            { name: "two"   }
            { name: "three" }
            { name: "four"  }
            { name: "five"  }
        ]

App.reqres.setHandler "navigation:entities", ->
    API.getLinks()

核心导航文件。

@TestApp.module "NavigationUnit", (NavigationUnit, App, Backbone, Marionette, $, _) ->
@startWithParent = false

API =
    listNavigation: ->
        NavigationUnit.List.Controller.listNavigation()

NavigationUnit.on "start", ->
    API.listNavigation()

控制器,我将集合传递给视图。

@TestApp.module "NavigationUnit.List", (List, App, Backbone, Marionette, $, _) ->

List.Controller =

    listNavigation: ->
        links = App.request "navigation:entities"

        navigationView = @getNavigationView links
        App.navRegion.show navigationView

    getNavigationView: (links) ->
        new List.Navigation
            collection: links

观点。

@TestApp.module "NavigationUnit.List", (List, App, Backbone, Marionette, $, _) ->

class List.NavigationLinks extends Marionette.ItemView
    template: "navigation/list/templates/_links"
    tagName: "li"

class List.Navigation extends Marionette.CompositeView
    template: "navigation/list/templates/list_navigation"
    itemView: List.NavigationLinks
    itemViewContainer: "ul"

ItemView模板的内容为%a{:href => "#"}= @name。在CompositeView中是一个带有%ul标记的基本包装结构。现在发生的是CompositeView按预期呈现模板,但它不会使用itemView填充%ul。相反,它创建的div数量等于集合中的模型数量(在本例中为5)并插入整个包装模板,所以看起来像这样:

#navRegion
  .div
    .navigation-wrapper
      .navigation-content
         %ul
     .div
        .navigation-wrapper
          // entire template
     .div
        .navigation-wrapper
          // entire template
//etc +3 divs

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

您的教程可能已过期。 Marionette在版本2.0.0中将该属性从itemView重命名为childView

来自the docs

  

每个childView都将使用childView的模板进行渲染。 CompositeView' s   渲染模板,并将childView的模板添加到此。

var ChildView = Marionette.ItemView.extend({});

var CompView = Marionette.CompositeView.extend({
  childView: ChildView
});