2

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 :)

FIDDLE

UPDATED FIDDLE I am making a little progress, I still am not sure how to apply relational here

FIDDLE

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.

Michael Joseph Aubry
  • 12,282
  • 16
  • 70
  • 135
  • Does my question make sense, has anyone used a collection within a collection? Seems like this is something nobody does, I cannot figure out why or how nobody does? – Michael Joseph Aubry May 05 '14 at 05:31
  • Another update supporting adding teams and players http://jsfiddle.net/4uLvC/25/ I hope this helps someone else. – Michael Joseph Aubry May 05 '14 at 06:58
  • Also a thing to note, the way I have built the fiddle backbone.relational actually helps my composite view.. when I call `this.collection = this.model.get('players')` it seems by having the relational models everything works smooth, when I revert back to normal backbone models I have problems, so I think having the relational models set I don't have to do any extra work. – Michael Joseph Aubry May 05 '14 at 07:10
  • I am not sure what is still missing from your fiddle. It pretty much seems to fit the "ideal example" you have added as comparison. If you have found the solution, feel free to add an answer to your own question. – Denis Washington Mar 09 '15 at 22:03

0 Answers0