0

我正在使用骨干关系来编写使用嵌套数据的应用程序。数据结构的顶层包含大部分数据(以 MB 为单位),但从不用作独立项;它仅用于充实其相关子项的属性(享元模式?)。

要将所有这些数据发送到浏览器,我正在使用类似于以下数据结构的东西

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

然后这由两个模型封装,Piece并且Performance- 都继承自 Backbone.RelationalModel 但只有Performance一个定义如下的关系

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

然后可以直接从原始 JSON 构建一个表演集合。

这一切都很好,但问题是我为每个数据发送了几个副本Piece(因为每个片段通常有几个表演)所以下载大小比它需要的大很多。

发送更轻量级的数据结构(如下所示或避免大量重复的其他结构)的最佳方式是什么,但仍然可以相对轻松地创建我需要使用的性能集合。

records = [ 
   {composerName: 'Beethoven', ..., performances: [array of jsons, one for each performance] 
   ... 
] 
4

1 回答 1

0

我最终这样做的方式是将数据发送为

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

在片断模型中定义以下关系

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

然后像往常一样建立一个表演收藏和一个作品收藏

于 2012-10-29T20:34:33.100 回答