总结骨干集合中的项目? [英] Wrap items in backbone collection?

查看:130
本文介绍了总结骨干集合中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直运行到一些容易混淆的解决方案和途径不明包裹比赛拖入一个div使用骨干的项目。

I keep running into some confusing solutions and unclear ways to wrap items that match into a div using backbone.

我只是建立一个简单的例子,我自己,并想窝在集合中具有相同属性的所有机型团队,使用比较运行良好的组织名单,但对我的生活我无法找到一个明确的解决方案,以每个包裹,让我有超过球员名单更多的控制权球队内线。

I am just building a simple example for myself, and would like to nest all models in a collection that have the same attribute team, using a comparator works well in organizing the list, but for the life of me I can't find a clear solution to wrapping each so that I have more control over the list of players inside the team.

必须有像我这样的初学者一个清晰简单的解决方案。我真的只是想保持的东西是干净和简单越好。我想要的HTML结果看起来像下面。

There has to be a clear easy solution for a beginner like me. I really just want to keep things as clean and simple as possible. My desired html result looks like below.

<div class="pacers">
    <li>Paul</li>
    <li>Roy</li>
</div>
<div class="bulls">
    <li>Kirk</li>
    <li>Taj</li>
</div>

根据骨干友好的JSON数组像下面的。

Based on a backbone friendly json array like below.

 [
  {
    "name": "Paul",
    "team": "pacers"
  },
  {
    "name": "Kirk",
    "team": "bulls"
  },
  {
    "firstname": "George",
    "team": "pacers"
  },
  {
    "name": "Taj",
    "team": "bulls"
  }
]

因此​​,使用比较是真棒我只是写这个比较:团队,它处理列表顺序对我来说,很酷,但我没有太多的控制我会想换一个更分级系统列表。

So using a comparator is awesome I just write this comparator : 'team' and it handles the list order for me, cool, but I dont have much control I would like to wrap the list in a more hierarchical system.

推荐答案

纠正我,如果我给它的型号不对所描述的问题涉及更多的控制关系的每个项目的内容,以及如何简单地使它们在组。

Correct me if I am wrong the problem being described relates more to controlling the contents of each item in relation to it's model as well as how to simply render them in groups.

1)NIRANJAN已覆盖分组出来的数据到单独列出,但请记住,返回此列表不是一个骨干结构。

1) Niranjan has covered grouping out the data into separate lists but remember that this list returned is not a Backbone construct.

2)按照手册中的_.groupBy'的方法应该通过集合即提供给您:

2) As per the manual the '_.groupBy' method should be available to you via the collection i.e.:

myCollection.groupBy(等);

myCollection.groupBy(etc);

3)我个人考虑GROUPBY的结果映射回模型,每一个模型传递到一个单独的视图,然后从主列表视图中绘制它们。

3) I would personally consider mapping the results of the groupBy back into models and pass each and every model into a separate view and render them from within the main list view.

var CollectionView = Backbone.View.extend({
    initialize : function () {
       // Note: I am pretending that you have a real collection.
       this.collection.fetch().then(
         this.addAll(true);
       );
    }

    addOne : function (model) {
      // call .render individual template items here for each model.
      var view = new ItemView(model);
      this.$el.append(view.render();
    },

    addAll : function (groupOpts) {
      var col = this.collection;

      if(groupOpts === true) { 
         // Do grouping (or do it in the model). Maybe put back into new collection?
      }         

      _.each(col, function(model) {
         this.addOne(model);
      }, this);
    },

    render : function () {
      // Render your template here.                    
    }
});

var ItemView = Backbone.View.extend({
   render : function () {

   }
});

不是一个完整的例子,但是一般的模式尝试同样的事情时,我将跟随。有每个项目的个人看法/型号,在我看来,给你更多的控制权。

Not a complete example but that's the general pattern I would follow when attempting the same thing. Having an individual view/model for each item, in my opinion, gives you more control.

这篇关于总结骨干集合中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆