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 = treeObject;
2. Traversing Tree
tree; // returns "body", `tree` is a backbone modelvar nodes = tree; // `nodes` is a backbone collectionvar sidebar = nodes; // `sidebar` is a backbone modelsidebar; // 300 treenextid; // "content"treeid; // "sidebar" // node hierarchysidebarparent === tree // truesidebar === tree // truetree; // truetree; // truesidebar; // false
3. Special Search
var content = tree; // `find` returns unique nodevar paragraphNode = tree; // `findWhere` returns first matchvar paragraphNodes = tree; // `where` returns all matches // TreeModel nodes inherit from Backbone Modelscontent; // "div"contentlength; // 1treelength; // 2 // Recursive-where for TreeCollectionvar nodes = tree;nodeslength; // 2nodeslength; // 3 // Special mixed TreeModel node Arrayvar specialArray = tree; // Array with standard methods (push/pop/splice/etc.)specialArraylength; // 3var array2 = specialArray; // has where method and returns special arrayarray2length; // 2
4. Adding Nodes
var sidebar = tree; // add single nodesidebarlength; // 1sidebar; // adds object to sidebar nodesidebarlength; // 2 // add array of objectssidebarlength; // 1sidebar;sidebarlength; // 3 // adding a node to the left of the current nodesidebar;sidebarid // "sidebar_left" // adding a node to the right of the current nodesidebar;sidebarnextid // "sidebar_right" // adding or inserting other nodes in the tree will perform a "move"var tree = treeObject; // start over with original datatree; // add existing nodetreelength; // 3treeparent == tree; // true
5. Removing Nodes
var tree = treeObject; // start over with original datatreelength; // 2tree; // remove sidebar nodetreelength; // 1treeid; // "content" var tree = treeObject; // start over with original datatreelength; // 2tree; // remove first matched spantreelength; // 1 var tree = treeObject; // start over with original datatreelength; // 2tree; // remove all matched nodestreelength; // 0
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