Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDIT</strong></p> <p>Added the jsfiddle: <a href="http://jsfiddle.net/ythLm/" rel="nofollow">http://jsfiddle.net/ythLm/</a></p> <p>Not sure if that's what you wanted to implement. I am assigning players to teams in this demo, which may or may not satisfy all your future requirements. (but we can always improve) :) </p> <p><strong>===ORIGINAL ANSWER===</strong></p> <p>I'm not sure I fully understand your requirements here...so you wanna display all the players in one view? and show their team names and sides? what's the order for the players list?</p> <p>but:</p> <p>to assign players to teams, you can just simply pass them as an attribute on the team model.</p> <p>Oh yea, I think You need a team model: </p> <pre><code>App.Models.Team = Backbone.Model.extend({ //team stuff }); App.Collections.Teams = ... //usually you want a collection too </code></pre> <p>and then</p> <pre><code>var blueTeam = new App.Models.Team({ name: 'blue team', players: new App.Collection.Players([ {"name": "Mike", "points": 10 }, { "name": "Joe", "points": 13 }, { "name": "Kobe", "points": 23 } ]); }); </code></pre> <p>Home team and away team, or just currently selected team, can just be an attribute on the team model. When you want to change them, you can just change the attribute on the team model. eg.</p> <pre><code>var blueTeam = new App.Models.Team({ //other attributes and players selected: true, side: 'home' }); </code></pre> <p>You can attach those status to collections as well, but I don't recommend doing that. (let's think if this is a real world application, Team has_many :players, and Player belongs_to :team)</p> <p>but you want to have all the players in one list (if i understand your requirements correctly), so we need to assign Team to each Player, instead of assigning players to a Team:</p> <pre><code>var mike = new App.Models.Player({ name: 'mike', team: new App.Models.Team({ name: 'blue' }); }); </code></pre> <p>but there's a problem with this: there are 20 players, but only 4 teams, you will have each team repeated 5 times, and they are all different objects. eg. if you do </p> <pre><code>mike.get('team').set('selected', true); </code></pre> <p>only mike's 'team' will be selected, other blue team players' "team" will not be updated.</p> <p>to solve this.</p> <p>First, get all the players altogether. remember, they need to a foreign key, or any kind of reference to the team they belong to: (it could be team_id, or just team_name)</p> <pre><code>var players = App.Collections.PlayersList([ {"name": "Mike", "points": 10, team_id: 1}, {"name": "Joe", "points": 13, team_id: 2}, {"name": "Kobe", "points": 23, team_id: 1} //... all your players ]); </code></pre> <p>and then, your teams, but you don't have to assign players :</p> <pre><code>var teams = new App.Collections.Teams([ {"id": 1, name: "blue team", selected: false, side: ''}, {"id": 2, name: "red team", selected: false, side: ''} //... all your teams ]); </code></pre> <p>now, somewhere in your app assign teams to your players:</p> <pre><code>players.each(function(player){ player.set("team", teams.get(player.get("team_id"))); }); </code></pre> <p>doing it this way, makes sure all the players on the same team will have the same team model.</p> <p>(Example <a href="http://jsfiddle.net/sbjaz/10/" rel="nofollow">http://jsfiddle.net/sbjaz/10/</a> when the code is paused by debugger, open your console, and try to play with player_1, player_2, and player_3, 1 and 2 are on the same team, try change any attributes on the team model for player1/2 and check the other one's team model)</p> <p>(another Example <a href="http://jsfiddle.net/sbjaz/11/" rel="nofollow">http://jsfiddle.net/sbjaz/11/</a> do the same, you will notice when you update the team for player_1, player_2's team is not updated)</p> <p>finally, in your collection view:</p> <pre><code>addOne: function(model) { var playerView; if(model.get("team").isHomeTeam()) { //isHomeTeam is a helper method in team model playerView = new App.Views.HomePlayer({ model: model }); } else { playerView = new App.Views.AwayPlayer({ model: model }); } this.$el.append( playerView.render().el ); } </code></pre> <p>well, and you need to pass Teams collection to PlayerList view as well so you know if any of the team has changed:</p> <pre><code>new playersView = new App.Views.Players({ collection: players, teams: teams }); </code></pre> <p>track team changes, in your view definition:</p> <pre><code>App.Views.Players = Backbone.View.extend({ initialize: function(options) { this.teams = options.teams; this.teams.on('change', this.render, this); //if any of the team changes, rerender everyting this.render(); } }); </code></pre> <p>and, you may think, this is not efficient at all: each time you swtich Home/Away teams, all the players will have to be re-rendered. how do I refactor this?</p> <p>well first, I think you don't have to define two views for the player view -- they have too much repeated code! it's not DRY. </p> <p>let's define a view that works for any player (at least for now):</p> <pre><code>App.Views.PlayerView = Backbone.View.extend({ tagName: "li", // events...mikeAlert they are all the same // you will see why we assign this.template in render instead of initialize //well or you don't have to assign this.template at all. render: function() { var templateSelector = this.model.isHomeTeam() ? "#home-template" : "#away-template"; this.template = _.template($(templateSelector).html()); return this.$el.html(this.template(this.model.toJSON());; } }); </code></pre> <p>you get the idea, or may be you don't even have to write two templates if they have too much repeated code too!</p> <p>well you need to update addOne:</p> <pre><code>//since PlayerView's render() returns html directly... addOne: function(model) { this.$el.append(new App.Views.PlayerView({model: model}).render()); } </code></pre> <p>now events, you don't have to pass teams to your PlayersView or bind collection change events there anymore. </p> <p>in your PlayerView's initialize:</p> <pre><code> initialize: function() { //rerender player view, when team is changed! this.model.on('change', this.render, this); } </code></pre> <p>now, every time you change any team, only the relevant playerViews will be updated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload