I have a situation where I need to save a couple of objects for a single person, but one object at a time, and when fetched, I get all the objects as an array along with the person's info.
This is how the model looks:
//Model for one save
{
personID: 1,
personName: 'John Doe',
score: [
{
subject: 'English',
marks: 30
}
]
}
Fetch returns the collection like this, and I only need to render the marks on the UI as a list:
//Collection of marks for a person
{
personID:1,
personName: 'John Doe',
score: [
{
subject: 'English',
marks: 30
},
{
subject: 'Maths',
marks: 30
},
{
subject: 'Geography',
marks: 30
}
]
}
The problem with this structure is that I cannot insert the model in the already rendered collection, since the structure is different hence the UI will not update.
How do I structure it in a better way? Or, can I treat the marks object as a model, and do the save/update using only the collection?
Collectionin Backbone with how the data is stored inside a model. The JSON as given above is representative of theUserin your system, which happens to have an array of score objects inside of it. The items in thescorearray don't have an ID, so attempting to treat them as a full-blownCollectionwill be difficult without adding that. Beyond that, keep in mind that Backbone does not (natively) support nesting aCollectioninside aModel. Can you provide some code showing theUsermodel?