backbone-tree-model
Tree data structure using Backbone Model and Collection
Installation
in nodejs
install the node module
npm install backbone-tree-model
then use it in your app
var BackboneTreeModel = require('backbone-tree-modle');
var tree = new BackboneTreeModel(treeObject);
in browser
see test/index.html
just append reference to src/backbone.treemodel.js
on your page after underscore and Backbone, then
var tree = new BackboneTreeModel(treeObject);
or
var tree = new Backbone.TreeModel(treeObject);
in browser AMD (using requirejs)
see test/amd.html
<script src="require.js"></script>
<script>
requirejs.config({
paths: {
jquery: 'path/to/jquery',
backbone: 'path/to/backbone',
underscore: 'path/to/underscore',
treemodel: 'path/to/backbone.treemodel',
}
});
define(['treemodel'], function(TreeModel) {
var tree = new TreeModel(treeObject);
});
</script>
Usage
1. Initialize
var treeObject = {
tagname: 'body',
nodes: [
{
id: 'sidebar',
tagname: 'div',
width: 300,
nodes: [
{ tagname: 'p' },
{ tagname: 'span' }
]
},
{
id: 'content',
tagname: 'div',
width: 600,
nodes: [
{ tagname: 'div' },
{
tagname: 'p',
nodes: [
{
tagname: 'anchor',
nodes: [
{ tagname: 'span' }
]
}
]
}
]
}
]
};
var tree = new Backbone.TreeModel(treeObject);
2. Traversing Tree
tree.get('tagname');
var nodes = tree.nodes();
var sidebar = nodes.first();
sidebar.get('width');
tree.find('sidebar').next().id;
tree.find('content').prev().id;
(sidebar.parent() === tree)
(sidebar.root() === tree)
tree.isRoot();
tree.contains(sidebar);
sidebar.contains(tree);
3. Special Search
var content = tree.find('content');
var paragraphNode = tree.findWhere({tagname: 'p'});
var paragraphNodes = tree.where({tagname: 'p'});
content.get('tagname');
content.where({ tagname: 'p'}).length;
tree.where({ tagname: 'p' }).length;
var nodes = tree.nodes();
nodes.where({tagname: 'div'}).length;
nodes.where({tagname: 'div'}, {deep: true}).length;
var specialArray = tree.where({tagname: 'div'});
specialArray.length;
var array2 = specialArray.where({tagname: 'span'});
array2.length;
4. Adding Nodes
var sidebar = tree.find('sidebar');
sidebar.where({tagname: 'p'}).length;
sidebar.add({tagname: 'p'});
sidebar.where({tagname: 'p'}).length;
sidebar.where({tagname: 'span'}).length;
sidebar.add([
{ tagname: 'span'},
{ tagname: 'span'}
]);
sidebar.where({tagname: 'span'}).length;
sidebar.insertBefore({id: 'sidebar_left'});
sidebar.prev().id
sidebar.insertAfter({id: 'sidebar_right'});
sidebar.next().id
var tree = new Backbone.TreeModel(treeObject);
tree.find('content').add(tree.find('sidebar'));
tree.find('content').nodes().length;
tree.find('sidebar').parent() == tree.find('content');
5. Removing Nodes
var tree = new Backbone.TreeModel(treeObject);
tree.find('wrapper').nodes().length;
tree.find('sidebar').remove();
tree.find('wrapper').nodes().length;
tree.find('wrapper').nodes().first().id;
var tree = new Backbone.TreeModel(treeObject);
tree.where({tagname: 'span'}).length;
tree.remove({tagname: 'span'}, true);
tree.where({tagname: 'span'}).length;
var tree = new Backbone.TreeModel(treeObject);
tree.where({tagname: 'span'}).length;
tree.remove({tagname: 'span'});
tree.where({tagname: 'span'}).length;
Contributing
Please ensure all current tests pass and write tests for new features.
Tests
You can run tests either on the browser or using the mocha command line tool.
For browser tests, open test/index.html
or test/amd.html
.
To run tests in the terminal
npm install
npm test