4

I would like to add new properties to DynatreeNode class. For example in methods OnSelect I want to use node.myproperty. node.myproperty value is calculated from some DOM element. Is there a way to do this? I tried using jQuery extend, but it does'nt seem to be possible.

matt137
  • 169
  • 1
  • 14

1 Answers1

0

this is JavaScript, so you can create a new attribute virtually everywhere, simply by assigning it ;-)

For Dynatree, the recommended place would be the node.data object, e.g.

node.data.foo = "bar";

then access it:

onSelect(node){
    if(node.data.foo !== undefined){
        alert(node.data.foo);
    }
}
mar10
  • 11,918
  • 5
  • 35
  • 58
  • And what if "foo" property should be accessible OnSelect, getNodeByKey, getActiveNode etc? How to make all node instances have property foo? – marcinn Feb 10 '12 at 22:43
  • if you assigned it, it's there. Otherwise it is `undefined`. I Edit the sample – mar10 Feb 11 '12 at 06:53
  • mar10, let "foo" be a function for example node.data.foo = function() { return $("#someTextBox").val() }. I would like use property "foo" in multiple contexts for example in onSelect, onActivate etc. The node should have property "foo" which is is always accessible not 'undefined'. – marcinn Feb 11 '12 at 12:33
  • You could add functions to the DynatreeNode prototype. This is rather a JavaScript question than a Dynatree question. – mar10 Feb 11 '12 at 16:25
  • actually, i was thinking about using DynaTreeNode.prototype, to implement a function like marcinn wrote, but the property I define this way is not being added. I try to do that in jquery ready() and in dynatree onrender(), but i get nothing. Perhaps you know what I might be doing wrong – matt137 Feb 12 '12 at 21:00