0

I setup this contextmenu

$('#myJsTree'). { .....
    , 'contextmenu': {
        'items': function (node) {
            if (mode != "Open") {
                var tmp = $.jstree.defaults.contextmenu.items();
                delete tmp.rename;
                delete tmp.remove;
                delete tmp.ccp;
                tmp.create.label = "New Folder";
                tmp.create.action = function (dta) {
                    console.log("so far so good");
                    $('#myJsTree').trigger('create_node.jstree', dta);
                }
                return tmp;
            }
        }
});

to trigger this event

$('#myJsTree').on('create_node.jstree', function (e, dta) {
    console.log("dta", dta);
});

And everything seems to so okay but the console also logs:

so far so good

as expected, and

dta Object {item: Object, reference: n.fn.init1, element: n.fn.init1, position: Object}

as expected. But also,

jstree.min.js:5 Uncaught TypeError: Cannot read property 'parent' of undefined

at a.jstree.plugins.contextmenu. (jstree.min.js:5)

at HTMLDivElement.d (jquery.js:2)

at HTMLDivElement.dispatch (jquery.js:3)

at HTMLDivElement.r.handle (jquery.js:3)

at Object.trigger (jquery.js:3)

at HTMLDivElement. (jquery.js:3)

at Function.each (jquery.js:2)

at n.fn.init.each (jquery.js:2)

at n.fn.init.trigger (jquery.js:3)

I can't understand why the error or what to do about it.

Any help appreciated.

Steve
  • 905
  • 1
  • 8
  • 32
  • Do you want to create a node and it doesn't work? Or is it something else? – Nikolay Ermakov Jan 28 '17 at 20:02
  • I am creating a node in the tree yes. This is basically the example filebrowser form jsTree. I'm creating a node & folder. But I do not have an need for the "delete, rename, move" menu options. I will not be creating a file. So I don't need any sub context menus. All of that is working just fine. I'm calling the .on('create_node.jstree') event with no problems. I can complete the rest of the event to actually create the node & the folder. I just have no idea why I'm getting this error. – Steve Jan 28 '17 at 20:40
  • Yes, that kind of strange, there is no errors in Fiddle - http://jsfiddle.net/ermakovnikolay/qagwejsc/. Only I didn't notice where you actually create the node, you just trigger the event. – Nikolay Ermakov Jan 28 '17 at 20:52
  • Thanks for digging into it and with a fiddle no less. In the example and what I intend to do is inside the .on('create_node.jsTree') triggered even an ajax get is made to have the back end code actually create the folder. if the ajax returns success then create the node in the jsTree. – Steve Jan 28 '17 at 21:56
  • @NikolayErmakov this is what you do inside the .on() event. Could setting the instance on the .don() be important? ` `$.get('/BOM_Builder/default/FolderBrowser?operation=create_node', { 'type': dta.node.type, 'id': dta.node.parent, 'text': dta.node.text }) .done(function (d) { dta.instance.set_id(dta.node, d.id); }) .fail(function () { dta.instance.refresh(); });` – Steve Jan 28 '17 at 21:57
  • Are you sure, the `dta` object has `node` property - as you pass it to your ajax call ? It is not there, when you console.dir it. Maybe this why you get the error saying `Cannot read property 'parent' of undefined` - because the `node` property is not defined in `dta` object ? – Nikolay Ermakov Jan 28 '17 at 22:13
  • If I echo the dta object to the console in both the call to the event and from in the event they appear to be the same object. Is there an easy way to save a comprehensive and complete copy of something from the inspect console tab to a file? I don't specifically see a node property in the console. – Steve Jan 28 '17 at 22:30
  • You get the full `dta` layout in console. You say, you don't see the `node` property in console and that was my question. You don't have it there, but you try to access it here in your code - `{ 'type': dta.node.type, 'id': dta.node.parent, 'text': dta.node.text }` and that might cause that error. – Nikolay Ermakov Jan 28 '17 at 22:34
  • @NikolayErmakov I added a pic of the console. This doesn't have an attribute specifically named "node" but it looks like a node to me. What do you think? – Steve Jan 28 '17 at 23:48
  • Can you chat about it? – Nikolay Ermakov Jan 29 '17 at 01:28
  • Otherwise, on the pic is a different node, that is a DOM node - the ul element. I meant the tree node that is referred to in your ajax call, but it is not available under the `dta` object, hence the error. – Nikolay Ermakov Jan 29 '17 at 01:52
  • I'll look into that tomorrow. It makes sense. So when you were speaking to "node" you meant jsTree node. You might have got this too – Steve Jan 29 '17 at 02:22

1 Answers1

0

I rebooted back to square one.

I found in jsTree is you are going to use the 'contextmenu' plugin then you also have to include the check_callback callback when you implemented the jsTree $('#s).jsTree({ ... , 'check_callback' });

I overlooked this detail originally.

And now if makes complete sense there the

"Cannot read property 'parent' of undefined

error was coming from.

Great thanks to Nikolay Ermakov for hanging in there with me.

Steve
  • 905
  • 1
  • 8
  • 32