0
votes

I'm creating my first backbone app. It's simply a list of teams and their players. I can display the list of teams. But i'm running into problems displaying the players of that team.

My Player model is as follows:

define(function (require) {

"use strict";

var $                   = require('jquery'),
    Backbone            = require('backbone'),


    Player = Backbone.Model.extend({

        urlRoot: "player",

        initialize: function () {
            this.players = new PlayerCollection();
            this.players.url = this.url + "/" + this.team_id;
        }  

    }),

    PlayerCollection = Backbone.Collection.extend({

        model: Player,
        url: "team_players",

    });

return {
    Player: Player,
    PlayerCollection: PlayerCollection
};

});

So the PlayerCollection is a collection of Players based on the team_id.

In the router I have:

            var playerList = new models.PlayerCollection({team_id: team_id});
            playerList.fetch();

But the GET URL sent is "http://mydomain.com/api/team_players" when I want the URL to be "http://mydomain.com/api/team_players/1" (with 1 being the team id). So how can I get the collection based on the team_id?

1

1 Answers

0
votes

Try this :

PlayerCollection = Backbone.Collection.extend({

    model: Player,
    url: function(){
       return "team_players/" + this.team_id;
    }

});

//use it like so

 var playerList = new models.PlayerCollection();
 playerList.team_id = team_id;
 playerList.fetch();