【问题标题】:Marionette CompositeView behaviorMarionette CompositeView 行为
【发布时间】:2015-08-31 06:30:10
【问题描述】:

我正在尝试遵循 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

我在这里做错了什么?

【问题讨论】:

  • 有什么问题?抛出错误?它不渲染?
  • 问题是我的 ItemView 被忽略,而是用 CompositeView 模板的副本填充#navRegion,就好像它们是模型一样。这里看起来像i.imgur.com/dLWJEPp.png

标签: backbone.js marionette


【解决方案1】:

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

来自the docs

每个 childView 都将使用 childView 的模板进行渲染。复合视图的 模板被渲染,childView 的模板被添加到这里。

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

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

【讨论】:

  • 感谢您的回复,不幸的是,当我将 itemViewitemViewContainer 分别更改为 childViewchildViewContainer 时,我的模板错误处理程序抛出了一个错误,而我的 ItemView 没有不再存在。我没有在文档中找到与不同模板方法相关的任何内容,我是否遗漏了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-08
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多