I’m working on a function that will return a context menu for use within a jsTree (http://www.jstree.com/documentation/contextmenu) context. In setting up the context menu object, I need to somehow get some vars into a wrapped call to another function in my integrated plugin.
The fiddle is here http://jsfiddle.net/9sKF5/12/ and the code is thus:
$(document).ready(function() {
var treeDef = {
"rateComponentGroup": {
title: "Rate Component Group",
entity: "rateComponentGroup",
contextMenu: [
{label: "New...", action: "new", entity: "rateComponentGroup"},
{label: "Delete...", action: "confirmdelete", entity: "rateComponentGroup"}
]
}
}
var clickedNode = "rateComponentGroup";
if (treeDef[ clickedNode ]) { // do we have setup for this node?
cntxtMenu = treeDef[ clickedNode ].contextMenu;
if (cntxtMenu instanceof Array) {
var menu = {};
for(var i = 0; i < cntxtMenu.length; i++) {
// append msg to html to see what we're doing
$("#message").append(cntxtMenu[i].label + cntxtMenu[i].action + "<br/>");
// define each context menu item label and action
menu[i] = {
label: cntxtMenu[i].label,
action: function() {
//
// how to get cntxtMenu[i].label and cntxtMenu[i].action in here?
//
}
}
}
}
// in larger context this block simply returns an object defining a context menu for jsTree
// containing a label and an action method to fire when selected
// see: http://www.jstree.com/documentation/contextmenu
// return menu;
}
});
You can copy the value of those objects into objects in your menu[] array. Then use their values in the action function()
Working example @ JS Fiddle