I am a complete javascript newbie, how can I integrate, jsTree on the front end, with the backend services in node.js. The backend is written using the Treemodel library (http://jnuno.com/tree-model-js/). With additional functions such as
function getChildren(x)
{
var result=[];
if(x.hasChildren())
{
for(i=0;i<x.children.length;i++)
{
result.push(x.children[i].model);
}
}
return result;
}
and
function expandAll(node) {
console.log(getChildren(node));
for (var t = 0; t < node.children.length; t++) {
if (node.children[t].hasChildren()) {
expandAll(node.children[t]);
}
}
}
My data is in initially in flat-text form :
var items = [
{'id': 2, 'parentid': 1, 'title': "Delhi"},
{'id': 3, 'parentid': 2, 'title': "CP"},
{'id': 4, 'parentid': 2, 'title': "Saket"},
{'id': 1, 'parentid': 0, 'title': "India"},
{'id': 5, 'parentid': 1, 'title': "Mumbai"},
{'id': 6, 'parentid': 5, 'title': "Andheri"},
{'id': 7, 'parentid': 5, 'title': "Worli"},
{'id': 8, 'parentid': 7, 'title': "Wankhede"}
];
That has been converted to JSON by using the following code with underscore.js-
unflatten = function( array, parent, tree ){
tree = typeof tree !== 'undefined' ? tree : [];
parent = typeof parent !== 'undefined' ? parent : { id: 0 };
var children = _.filter( array, function(child){ return child.parentid == parent.id; });
if( !_.isEmpty( children ) ){
if( parent.id == 0 ){
tree = children;
}else{
parent['children'] = children
}
_.each( children, function( child ){ unflatten( array, child ) } );
}
return tree;
}
items = unflatten(items);
I will be implementing a tree structure in the front end with AJAX lazy loading, something very similar to: http://thejackalofjavascript.com/file-browser-with-jstree-angularjs-and-expressjs/
I just need help in understanding how the jsTree will be implemented with TreeModel, and how to make the lazy loading happen with the getChildren method implemented in the backend.
Thanks
Here is a simple example using restify server.
1) Create a project folder with the following
package.jsonfile:2) Run
npm install.3) Create a node script
server.jswith the following content4) Create a
publicfolder to place yourindex.htmlfile5) Create the
index.htmlfile insidepublicfolder with the following content:6) Run
npm startand browse to localhost:8080/public