Question: I am using backbone.js I have a player
view for one player, the root element for this view is a <li></li>
list item node.
Then I have a players
view that shows all the players in the collection, the root element for this view is a <ul></ul>
. (Pretty simple stuff!)
The result for this would be.
<ul> //The players collection root for the players collection view
<li>Kobe Bryant is on the lakers</li> // The player model root element for the player model view
<li>LeBron James is on the heat</li> // The player model root element for the player model view
</ul>
My model's attributes consist of name, team
.
[
{name: 'Kobe Bryant', team: 'lakers'},
{name: 'LeBron James', team: 'heat'}
]
How do I loop (or do whatever is best) so that my view results like this.
<ul class="lakers">
<li>Kobe Bryant is on the lakers</li>
</ul>
<ul class="heat">
<li>LeBron James is on the heat</li>
</ul>
Even Better wouldn't I need to do a teams collection view. Then write some code to match each player to his team? So a result something like this.
<div class="lakers">// Teams collection root element
<ul>// Players collection root element
<li>Kobe Bryant is on the lakers</li>// Player model root elemnt
</ul>
</div>
<div class="heat">// Teams collection root element
<ul>// Players collection root element
<li>LeBron James is on the heat</li>// Player model root elemnt
</ul>
</div>
I took an attempt at this but I am thinking the way I am instantiating each view is wrong, or I am just doing something wrong, so if it's a good idea to put a collection of players in a collection of teams, how do I get a result like above, where I can have a parent element which is the team, then a list of the players for that team inside?
My Ideas & Code: I am thinking inside the players
view I can write some stuff to organize each model by team, like.
_.each(this.collection.where({team: "lakers"}), function(model) {
var playerView = new PlayerView({ model: model});
this.$el.append(playerView.render().el);
this.$el.addClass('lakers');
}, this);
Well there are obvious issues with this because, we write all this just to add a class to the root, and if we did this again it would keep adding to the root <ul class="lakers miami">
That doesn't help.
I am running low on ideas, I get the picture I know I want to categorize each player but I cant quite figure it out. I am leaning towards a teams collection of players collection, I am just confused how to match this.team.get('name') == this.player.get('name')
//Something relative to this. I am not sure if that actually makes sense, ill admit I need help lol. Happy Thanksgiving everyone, I doubt anyone is on stackoverflow today.