0

I'm using backbone-relational to write an app using nested data. The top level of the data structure contains most of the data (in terms of MB) but is never used as a standalone item; it's only used to flesh out the attributes of its related children (flyweight pattern?).

To send all this data to the browser I'm using something like the following data structure

records = [ 
   {performerName: 'Jane', details: {composerName: 'Beethoven', ... }}, 
   ... 
] 

And this is then encapsulated by two models, Piece and Performance - both inherit from Backbone.RelationalModel but only Performance has a relation defined as follows

relations: [
    {
        type: 'HasOne',
        key: 'piece',
        relatedModel: 'Piece',
        reverseRelation: {
            key: 'performances'
        }
    }
],

and then a performances collection can be built straight from the original JSON.

This all works fine, but the problem is that I'm sending several copies of the data for each Piece (as each piece typically has several performances) so the download size is a lot bigger than it needs to be.

What's the best way to send a lighter weight data structure ( as below or some other structure that avoids masses of duplication) and yet still have relatively painless creation of the performances collection I need to work with.

records = [ 
   {composerName: 'Beethoven', ..., performances: [array of jsons, one for each performance] 
   ... 
] 
tshepang
  • 12,111
  • 21
  • 91
  • 136
wheresrhys
  • 22,558
  • 19
  • 94
  • 162

1 Answers1

0

The way I did this in the end was to send the data as

var data = { pieces: [
              {tune_name: 'eidelweiss', ..., performances: [{id:23}, {id:52}]} 
              ... 
             ],
             performances: [ array of performance jsons ]
           };

In the pieces model define the following relation

relations: [
    {
        type: 'HasMany',
        key: 'performances',
        relatedModel: 'Performance',
    includeInJSON: false,
        reverseRelation: {
            key: 'piece'
        }
    }
]

and then just build a performances collection and a pieces collection as normal

wheresrhys
  • 22,558
  • 19
  • 94
  • 162