UPDATE I am still not 100% but I made a fiddle that seems to be working well NEWWORKINGFIDDLE I am still testing it to see if there are any caveats, but this seems to work great, looks like I can drop backbone relational and Deep model :) All I needed was composite view this whole time, thanks Derrick Bailey
I have a collection of teams and players. Go ahead checkout the FIDDLE to see more of what I am trying to do, it should give a better idea of what I am doing.
var Relational = require('backbone.relational');
module.exports = TeamModel = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'players',
relatedModel: 'PlayerModel',
collectionType: 'PlayersCollection',
reverseRelation: {
key: 'playsOn',
includeInJSON: 'id'
}
}]
});
Then the player model
module.exports = PlayerModel = Backbone.RelationalModel.extend({});
I am basically good to go I can do things like this
var bulls = new TeamModel({ name: 'bulls' });
var michael = new PlayerModel({ position: 'Point Guard', playsOn: bulls });
console.log( bulls.get( 'players' ).pluck( 'position' ) );
console.log( michael.get( 'playsOn' ).get( 'name' ) + ', ' + bulls.get( 'players' ).length );
I finally have a simple understanding of backbone relational, but I am not sure how to build a view with marionette the way I want it, that is to have something like
<div class="team bulls">
<div class="the-players">
<div class="player michael"></div>
</div>
</div>
<div class="team warriors">
<div class="the-players">
<div class="player steph"></div>
</div>
</div>
I wrote a fiddle, but struggled a bit to get all the parts to work I had to do it from scratch since my actual application is much bigger and is modular so here is a basic idea FIDDLE
I am just trying to use backbone relational so that I can have a team element/view with a list of players related to that team within the view, I had a good setup using a nested model but that caused a lot of problems in adding/updating players.
P.S I have succesfully done a collection of teams and players merging them together, but it felt hackish and everyone says use relational so here I am, there are just some aspects I cant grasp, so hopefully someone can point it out :)
UPDATED FIDDLE I am making a little progress, I still am not sure how to apply relational here
EDIT: here is how I had it before, FIDDLE the problem is updating the collection for a specific team, I may just open a new question on that and see which solution is better the relational way, or perhaps the fiddle is a good way I just need a better way of updating and saving.