I have a list of categories. Each category can have sub categories. This is the model I've created:
App.Category = Em.Object.extend({
id: null,
name: null,
subCategories: Em.ArrayProxy.create({content: []})
});
The problem is, if I push a sub category into the subCategories array, it is pushed into every instance of App.Category
Take a look at this jsFiddle.
I don't understand why this is happening, and how should I rewrite this to work properly.
Code:
App.Category = Em.Object.extend({
id: null,
name: null,
subCategories: Em.ArrayProxy.create({content: []})
});
App.categories = Em.ArrayController.create({
content: [],
// assumes list of categories is ordered by parent id
loadCategories: function(cats) {
for (var i=0, cat; i < cats.length; i++) {
var parentID = cats[i].parentID;
delete cats[i].parentID;
cat = App.Category.create(cats[i]);
if (parentID !== null) {
var parent = this.findProperty('id', parentID);
parent.get('subCategories').pushObject(cat);
} else {
this.pushObject(cat);
}
};
}
});