Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try representing the tree with <code>Model</code> as the node and each node containing a <code>Collection</code> of its child nodes: </p> <pre><code>var CTreeDataItem = Backbone.Model.extend({ initialize: function() { if (Array.isArray(this.get('items'))) { this.set({items: new CTreeDataItemChildren(this.get('items'))}); } } }); // children collection var CTreeDataItemChildren = Backbone.Collection.extend({ model: CTreeDataItem }); // create var treeData = new CTreeDataItemChildren(treeJs); // access treeData.at(0).get('items').at(1).get('title') // returns "Item2" </code></pre> <hr> <p><strong>EDIT 2011-05-18:</strong> If you want to flatten the tree and maintain a reference to the parent that each node had in the tree:</p> <pre><code>// flatten the tree outside of Backbone. Alternatively, // you could override the constructor of the CTree collection // to provide encapsulation function flatten(parentID, arr) { var out = []; for (var i = 0; i &lt; arr.length; i++) { var node = arr[i]; node.parentID = parentID; out.push(node); if (Array.isArray(node.items)) Array.prototype.push.apply(out, flatten(node.id, node.items)); delete node.items; } return out; } // remove above code that would have provided nesting var CTreeDataItem = Backbone.Model.extend({}); // children collection, as before var CTreeDataItemCollection = Backbone.Collection.extend({ model: CTreeDataItem }); // create var treeData = new CTreeDataItemChildren(flatten('', treeJs)); // as before, but now there's the 'parentID' FK treeData.at(3).get('parentID') // returns "ti1" </code></pre> <p>Hope that's what you're after.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload