Marionette CompositeView 行为
Marionette CompositeView behavior
我正在尝试遵循 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
我做错了什么?
您的教程可能已过时。 Marionette 在 2.0.0 版中将 属性 从 itemView
重命名为 childView
。
来自 the docs
Each childView will be rendered using the childView's template. The CompositeView's
template is rendered and the childView's templates are added to this.
var ChildView = Marionette.ItemView.extend({});
var CompView = Marionette.CompositeView.extend({
childView: ChildView
});
我正在尝试遵循 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
我做错了什么?
您的教程可能已过时。 Marionette 在 2.0.0 版中将 属性 从 itemView
重命名为 childView
。
来自 the docs
Each childView will be rendered using the childView's template. The CompositeView's template is rendered and the childView's templates are added to this.
var ChildView = Marionette.ItemView.extend({});
var CompView = Marionette.CompositeView.extend({
childView: ChildView
});