2014-09-02 99 views
0

我可以使用嵌套复合视图来制作像这样的输出吗?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); 
} 
+0

所以你有一个品牌的集合,每个品牌都有体育活动的集合,你必须渲染它像一个嵌套列表? – Evgeniy 2014-09-02 06:24:37

+0

是的,这只是一个例子。我必须做类似的事情。它现在正在工作,但我不知道这是否正确的方式来做到这一点。 – Fingercross 2014-09-02 06:33:33

+1

结帐这篇文章 - 它非常接近你要找的东西http://stackoverflow.com/questions/25232982/backbone-fetch-related-models/25236715#25236715 – Evgeniy 2014-09-02 06:44:30

回答

0

是的,它是可能的。

方法我已经使用树形结构 - (?收集超过1名儿童)article.

0

怎么样,如果我想使这个

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> 
0

其实我只是写了一个答案,这可能有助于你几天前。看看这个线程 - Construct views for data nested several levels deep

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