How to place a collection within a model in Backbone.js?

前端 未结 2 2162
予麋鹿
予麋鹿 2021-02-19 17:48

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

2条回答
  •  执念已碎
    2021-02-19 18:19

    "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."

    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.

提交回复
热议问题