如何放置集合中Backbone.js的模型中? [英] How to place a collection within a model in Backbone.js?

查看:163
本文介绍了如何放置集合中Backbone.js的模型中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想就怎么一会在模型中添加结构集合一些见解。我简单的应用程序有球队(所以一个团队模式和团队的集合),每队有一堆球员(球员的典范和球员集合)。所以它的视觉结构是像这样:

I would like some insight on how one would add structure a collection within a model. My simple app has teams (so a team model and collection of teams) and each team has a bunch of players(player model and player collections). So a visual structure of it is like so:

Team A
   - Player 1
   - Player 2
   - Player 3
Team B
   - Player 1
   - Player 2

等等...

如何将一种结构这样的骨干应用程序?这里是我如何到目前为止计划吧:
1)我想有一个团队集合,将持有的多个团队的模型属性对应于TeamModel。
2)玩家收集,将持有的所有多个玩家和型号属性对应于PlayerModel。

How would one structure such a backbone app? Here is how I am planning it so far: 1) I would have a Team Collection, that would hold multiple teams whose model property corresponds to the TeamModel. 2) A Player Collection, that would hold all multiple players and model property corresponds to the PlayerModel.

现在我很困惑,我怎么会对团队收集和模型,带玩家收集和型号相对应。即按照我的设计,第三个关系是每个团队将有玩家的集合。但是我不确定如何实现这一点。

Now I am confused as to how I would have the Team Collection and Model, correspond with the Player Collection and Model. I.e. according to my design, a third relationship would be that each team would have a collection of players. However I am unsure of how to implement that.

推荐答案

现在我很困惑,我怎么会对团队收集和模型,带玩家收集和模型。即相当于按照我的设计,第三个关系是每个团队将有玩家的集合。

只是一个属性添加到您的团队模式,会是玩家的集合。

Simply add an attribute to your Team Model that'd be a collection of players.

var Team = Backbone.Model.extend({
  initialize: function() {
    // assuming Players a collection of players
    this.set('players', new Players());
  }
});

现在,填充数据是有很多的解决方案的另一问题。但做的事情,这样给你一个坚固的结构。

Now, populating the data is another problem which has a lot of solutions. But doing things that way gives you a strong structure.

这篇关于如何放置集合中Backbone.js的模型中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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