Class zebkit.data.TreeModel
mixed with <zebkit.data.DataModel> <zebkit.EventProducer>
<zebkit.data>

Tree model class. The class is simple and handy way to keep hierarchical structure.

Constructor:
zebkit.data.TreeModel ([r])

Parameters:
  • [r] <zebkit.data.Item | Object>

    a root item. As the argument you can pass "zebkit.data.Item" or a JavaScript object. In the second case you can describe the tree as it is shown in example below:


Example:
    // create tree model initialized with tree structure passed as
    // special formated JavaScript object. The tree will look as follow:
    //  "Root"
    //    |
    //    +--- "Root kid 1"
    //    +--- "Root kid 2"
    //            |
    //            +--- "Kid of kid 2"
    var tree = new zebkit.data.TreeModel({
       value:"Root",
       kids: [
           "Root kid 1",
           {
               value: "Root kid 2",
               kids:  [ "Kid of kid 2"]
           }
       ]
    });
    ...
    // reg item modified events handler
    tree.on("itemModified", function(tree, item, prevValue) {
        // catch item value modification
        ...
    });

    // item value has to be updated via tree model API
    tree.setValue(tree.root.kids[0], "new value");



public void add ([to], an)
public static <zebkit.data.Item> create (r)
public static <Array> find (root, value, [cb])
public static <zebkit.data.Item> findOne (root, value)
public void insert (to, an, i)
public void iterate (r, f)
public void remove (item)
public void removeKids (item)
public void setValue (item, v)


public void add ([to], an)

Add the new item to the tree model as a children element of the given parent item


Parameters:
  • [to] <zebkit.data.Item>

    a parent item to which the new item has to be added. If it has not been passed the node will be added to root.

  • an <Object | zebkit.data.Item>

    item or value of the item to be added to the parent item of the tree model

public static <zebkit.data.Item> create (r)

Create tree model item hierarchy by the given JavaScript object.


Parameters:
Returns: <zebkit.data.Item>

a built items hierarchy


Example:
 // create the following items hierarchy:
 //  "Root"
 //    +--- "Kid 1"
 //    |      +--- "Kid 1.1"
 //    |      |       +--- "Kid 1.1.1"
 //    |      +--- "Kid 2.2"
 //    +--- "Kid 2"
 //    |        +--- "Kid 2.1"
 //    |        +--- "Kid 2.2"
 //    |        +--- "Kid 2.3"
 //    +--- "Kid 3"
 //
 var rootItem = zebkit.data.TreeModel.create({
     value : "Root",
     kids  : [
         {   value : "Kid 1"
             kids  : [
                 {  value: "Kid 1.1",
                    kids : "Kid 1.1.1"
                 },
                 "Kid 2.2"
             ]
         },
         {   value: "Kid 2",
             kids : ["Kid 2.1", "Kid 2.2", "Kid 2.3"]
         },
         "Kid 3"
     ]
 });
public static <Array> find (root, value, [cb])

Find all items (starting from the specified root item) whose value equals the given value.


Parameters:
  • root <zebkit.data.Item>

    a root item of the tree

  • value <Object>

    a value to evaluate

  • [cb] <Function>

    a callback method that is called for every tree item whose value matches the specified one. The method gets the found item as its argument. The method can return true if the tree traversing has to be interrupted.


Returns: <Array>

a list of all found item whose value matches the specified one. The array is returned only if no callback method has been passed to the method.


Example:
 // create tree items
 var rootItem = zebkit.data.TreeModel.create({
     value: "Root",
     kids : [ "Kid 1", "Kid 2", "Kid 1", "Kid 3", "Kid 1" ]
 });

 // find all items that have its value set to "Kid 1" and return
 // it as array
 var items = zebkit.data.TreeModel.find(rootItem, "Kid 1");

 // find the first two "Kid 1" item in the tree using callback
 var items = [];
 zebkit.data.TreeModel.find(rootItem, "Kid 1", function(item) {
     items.push(item);

     // stop the tree traversing as soon as we found two items
     return items.length > 1;
 });
public static <zebkit.data.Item> findOne (root, value)

Find the first tree item (starting from the specified root item) whose value equals the given value.


Parameters:
Returns: <zebkit.data.Item>

a found tree item

public void insert (to, an, i)

Insert the new item to the tree model as a children element at the given position of the parent element


Parameters:
  • to <zebkit.data.Item>

    a parent item to which the new item has to be inserted

  • an <Object | zebkit.data.Item>

    item or value of the item to be inserted to the parent item

  • i <Integer>

    a position the new item has to be inserted into the parent item

public void iterate (r, f)

Iterate over tree hierarchy starting from its root element


Parameters:
  • r <zebkit.data.Item>

    a root element to start traversing the tree model

  • f <Function>

    a callback function that is called for every tree item traversed item. The callback gets tree model and the item as its arguments

public void remove (item)

Remove the given item from the tree model


Parameters:
public void removeKids (item)

Remove all children items from the given item of the tree model


Parameters:
  • item <zebkit.data.Item>

    an item from that all children items have to be removed

public void setValue (item, v)

Update a value of the given tree model item with the new one


Parameters:

public <zebkit.data.Item> root

Reference to the tree model root item


itemInserted

Fired when the tree model item has been inserted into the model

tree.on("itemInserted", function(src, item) {{
   ...
});

Parameters:
itemModified

Fired when the tree model item value has been updated.

tree.on("itemModified", function(src, item, prevValue) {
    ...
});

Parameters:
itemRemoved

Fired when the tree model item has been removed

tree.on("itemRemoved", function(src, item) {
   ...
});

Parameters: