I am having a little problem i am using a implementation of: helper function for nesting backbone collections. · GitHub
Slightly altered to include the ability to keep track if the model has any childcollections. I am currently at the point that i get a array with an array inside of it matching the structure you expect for the tree data:
[{ id: 1, value: 'test123', data: [{id: 2, value; 'test234'}] }]
Now my question is how can i get the tree to sync this data? So it adds the new child notes / other tree nodes that were included into the array.
I currently do this:
var self = this;
var treeData = self.treeArrayAdapter.createResponse();
//which returns the data above (including all the other model attributes
self.getChild("client-tree").sync(treeData);
But this throws an error since the sync function has a bind event handler for backbone:
bData.bind("change", function(model, info){
var cid = _get_id(model);
var ev = wData.pull[cid] = copymodel(model);
ev.id = cid;
if (wData._scheme_update)
wData._scheme_update(ev);
wData.refresh(ev.id);
});
This works fine when its a normal backbone collection but when i try to pass an array through it the bData (collection) is empty and thus throws the following error: Uncaught TypeError: undefined is not a function
Any suggestions would be welcome maybe trying to create a special array format is not the best idea and solutions based on the collection with sub-collections would be more suited in the current webix code for backbone binding.
Snipped how the current Array is created:
loopOverCollection: function(collection) {
var self = this;
var data = [];
collection.each(function(model) {
if(!model.toJSON) return;
var newData = model.toJSON();
if(model.hasCollections())
{
var childCollections = model.hasCollections();
_.each(childCollections, function(childCol) {
if(!model.get(childCol)) return;
if(!self.loopOverCollection(model.get(childCol))) return;
newData['data'] = self.loopOverCollection(model.get(childCol));
});
}
data.push(newData);
});
return data;
}