add(id, [data, parent])

Adds a node to Treeful object.

Parameters

  • id (string) - unique string id.
  • data (anything but function, optional) - data to be stored in the node. It can be in any type except function. Default is null.
  • parent (string, optional) - string id of a parent node. Default is root.

Returns

  • (object) - Treeful object. Allows add to be called in a chain.

Examples

Tip 1: Use chain function for simplicity.

import Treeful from 'treeful';

Treeful.add('n1')           // Adds node 'n1' with data null to parent 'root'
    .add('n2', 'value')     // Adds node 'n2' with data 'value' to parent 'root'
    .add('n3', null, 'n1'); // Adds node 'n3' with data null to parent 'n1'

Tip 2: Spaces may be used to help visualizing tree.

import Treeful from 'treeful';

Treeful.add('n1')
       .add('n2')
           .add('n3', null, 'n2')
           .add('n4', null, 'n2')
       .add('n5')
       .add('n6')
           .add('n7', null, 'n6')
               .add('n8', null, 'n7')
           .add('n9', null, 'n6');

Tip 3: Tree must be created before any other functions are called. DO NOT call add dynamically at different points.

import Treeful from 'treeful';

Treeful.add('page1')
           .add('isLoaded', false, 'page1')
           .add('count', 0, 'page1')
       .add('page2')
           .add('userInput', null, 'page2');

results matching ""

    No results matching ""