I've been scouring the web for a week and I'm stuck.
I'm creating a Backbone Relational model as follows:
SearchPage = Backbone.RelationalModel.extend({
urlRoot: '/api/search',
parse: function(response) {
return {
query: response.GSP.Q,
param: response.GSP.PARAM,
total: response.GSP.RES.M,
result: response.GSP.RES.R,
filter: response.GSP.RES.PARM.PMT.PV
};
},
relations: [
{
type: Backbone.HasMany,
key: 'result',
relatedModel: 'Result',
collectionType: 'ResultCollection',
reverseRelation: {
key: 'resultOf'
}
},
{
type: Backbone.HasMany,
key: 'filter',
relatedModel: 'Filter',
collectionType: 'FilterCollection',
reverseRelation: {
key: 'filterOf'
}
},
{
type: Backbone.HasMany,
key: 'param',
relatedModel: 'Param',
collectionType: 'ParamCollection',
reverseRelation: {
key: 'paramOf'
}
},
]
});
The creation from the JSON is working fine. All of the models and collections are being created perfectly fine, but I'm all seeing an extra Collection being created for each of the attributes that are being made into collections.
See the following grabbed from the Backbone inspector:
- Collection 0: Backbone.Collection {length: 0, models: (...), _byId: Object, url: (...), trigger: function…}
- Collection 1: Backbone.Collection {length: 10, models: (...), _byId: Object, url: (...), trigger: function…}
- Collection 2: child {length: 10, models: (...), _byId: Object, url: (...), trigger: function…}
- Collection 3: Backbone.Collection {length: 84, models: (...), _byId: Object, url: (...), trigger: function…}
- Collection 4: child {length: 84, models: (...), _byId: Object, url: (...), trigger: function…}
- Collection 5: Backbone.Collection {length: 15, models: (...), _byId: Object, url: (...), trigger: function…}
- Collection 6: child {length: 15, models: (...), _byId: Object, url: (...), trigger: function…}
There is no duplication of Models going on, so I'm a bit confused. The only thing I can think is that the child collections are a part of the attributes in the SearchPage Model?
Is there a way to prevent these extra collections from being created or are they a necessary part of the Model?
I guess some context for why I'm using such a big relational model is that the api only serves the information from one single URL and it's one giant blob of information that I parse into this model.
Thanks in advance for your help!
Aucun commentaire:
Enregistrer un commentaire