Below is a code that describes nested tree model, and a sample data in json.
How do I reset blocks with provided data?
var Block, Blocks, data, blocks;
Block = Backbone.Model.extend({
initialize: function() {
this.blocks = new Blocks;
}
});
Blocks = Backbone.Collection.extend({
model: Block
});
data = [
{
id: 1,
blocks: [
{id: 2, blocks: [{id: 3}]}, {id: 4}
]
},
{
id: 5
}
];
blocks = new Blocks;
Ok, well, since attributes are stored under
attributesprior to initialize, you need to callthis.attributes.blocksin your initialize function to ensure there is no mix-up withthis.blocks(which there was).You also need to initialize
blocksby passing data into it’s constructor, which is the same for every other instance ofBlocks, i.e., those also stored inside eachBlock.