木偶嵌套复合视图

时间:2014-09-02 06:22:02

标签: backbone.js marionette

我可以使用嵌套复合视图来生成这样的输出吗?

Nike
  Football, Basketball
Reebok
  Football, Basketball, Running

我确实用这种方式编写了嵌套复合视图。我不确定是否有其他更好的方法或最佳实践方式来实现它。

结构:

BrandCompositeView
   itemView: Layout1
                #brand<-BrandView (Nike, Reebok)
                #sport<-SportCompositView 
                           itemView: SportView 


BrandCompositeView (generate Nike, Reebok),
itemView of BrandCompositeView is a layout1 with div id are(#brand, #sport)

SportCompositView (generate Football, Basketball, Running)
itemView of SportCompositView is SportView

BrandView and SportView are itemView

Layout1 is Marionette.Layout

在Layout1.js

initialize:function(){
  this.brandView = new BrandView({"model": this.model});
  this.sportView = new SportCompositView({"collection":collection});
}

onRender: function () {
   this.brand.show(this.brandView);
   this.sport.show(this.sportView);
}

3 个答案:

答案 0 :(得分:0)

是的,这是可能的。

我用于树结构的方法 - article.

答案 1 :(得分:0)

如果我想制作这个(超过1个孩子的集合?)

Car: [
    {
    Car1
       name: car1
       image: [{img1}, {img2}, {img3}]
       comment: [{comment1}, {comment2}, {comment3}]
       price: $1000
    },
    {
    Car2
       name: car1
       image: [{img1}, {img2}, {img3}]
       comment: [{comment1}, {comment2}, {comment3}]
       price: $2000
    }
]


<script id="car-template" type="text/template">
  <li><%= name %></li>
  <div id="photo-container"></div>
  <li><%= price %></li>
  <div id="comment-container"></div>
</script>

答案 2 :(得分:0)

我其实只是写了一个可能在几天前​​帮助你的答案。看看这个主题 - Construct views for data nested several levels deep

基本上,它使用骨干关系来自动处理所有嵌套。然后,您可以从复合视图内部的复合视图中抽象出来。