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

前端 未结 2 2143
予麋鹿
予麋鹿 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:14

    You could do something like:

    App.Models.Player = Backbone.Model.extend({});
    
    App.Collections.Players = Backbone.Collection.extend({
        model: App.Models.Player,
        url: 'players',
        getTeam: function(idTeam){
            var gf = _.filter( this.models, function(model){
        return (
            model.get('idTeam') == idTeam
        );
        });
            return gf;
        }
    });
    
    App.Models.Team = Backbone.Model.extend({
        players: players( this.get('id') ) // asuming that players is an App.Collections.Players instance.
    });
    
    App.Collections.Team = Backbone.Collection.extend({
        model: App.Models.Team,
        url: 'teams'
    });
    

    And then, when you create the instances of each and collect data from the server, start the router once all collections have been populated. It should work that way.

提交回复
热议问题