开发者

jstree context menu action evaluation

So I've come across the following problem. I need to create a dinamic menu for my jstree. So:

                var default_dict = { 
                                create: false, 
                                rename: false, 
                                remove: false, 
                                ccp: false
                              };    
                for (one in viewers) {
                    var details = viewers[one];
                    alert(details['module']);
                    alert(details['classname']);          
                    default_dict[one] = {                                   
                                        label: 'View in: ' + one,
                                        action: function (obj) { 

                                                    doLaunch($project.id, nodeId, obj.one.module, obj.one.classname, obj.attr("dataType"))
                                               },
                                        seperator_after: false,
                                        seperator_before: false
                                    }
                }
                return default_dict;

Now this is the main part of my createMenuItems function which returns my menu items. Now I've checked and the module and classnames are fine in the:

 alert(details['module']);
 alert(details['classname']);

But as you can see I want the action of each item to be a launch with it.s correspondig module and classname data. However from what I can see the actual evaluation of: acti开发者_运维技巧on:

function (obj) { 

 doLaunch($project.id, nodeId, obj.one.module, obj.one.classname, obj.attr("dataType"))
                                           }, is done after the initialization of the tree. So now I'm in the following situation:

Assume viewers is the following:

{ 'viewer1' : { 'module': 'Module_one', 'classname' : 'Classname_one' }, 'viewer2' : { 'module' : 'Module_two', 'classname' : 'Classname_two' } }

In the tree initialization this is parsed properly and I get the items labeled: 'View in viewer1' and 'View in viewer2'. However when I click on 'View in viewer1' and the action function is evaluated, the details now contains Viewers2 module and classname and so the wrong launch is done. I hope I explained my problem properly.

Now I'm a beginned in js so maybe I'm missing something basic. Any suggestions would be appreciated.

Regards, Bogdan


Ok I made this work but only by using an eval to get the parameters evaluated at creation time. So my solution looks like:

eval("default_dict[one] = {label: '"+displayName+"', action: function(obj) {" + 
                 "doLaunch('" + url +"','"+ paramName+"', dataId);}}");


you can use a closure here on the action function. for example:

action: function(obj) {
  var one = obj.one;//this will save the obj instance, even if later the action function is called with the jsTree node obj
  return function(){
    doLaunch(url, one.module, one.classname);//only this will get called 
  };
}(obj);//this will call your function on each item in for loop
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜