I have specific troubles when I try create node. From context menu everything gona be OK, but when I call create event from button I don’t have AJAX request.
Look:
bind("create.jstree", function (e, data) {
console.log('creating');
$.ajax({
type: 'post',
url: '/item/add',
data: {
name: data.rslt.name,
id_menu_content_parent: data.rslt.parent.attr("id").replace("node_",""),
id_type_object: data.rslt.obj.attr("rel")
},
success: function (dataItem){
}
});
console.log('end');
});
And now when I call from context menu is OK, I see sting creating, next AJAX request and finally string end, but when I click the button I see only string creating and nothing more.
What should I do with this?
The most likely reason that your create works when invoked from your context menu but not when invoked with a button is that for your context menu, you probably have a tree node selected when the create callback is invoked. However, when you invoke the create callback from a button, you might not necessarily have a node selected. This will most likely cause your
$.ajaxcall to fail because it tries to reference undefined properties. Specifically,data.rslt.parent.attr("id")will fail because if you do not have a node selected, your new node will be created at the parent level and thusdata.rslt.parentwill have a value of-1. This should explain why you are not seeing yourconsole.log('end')output.From your question I’m guessing you are not using the debugger to debug your javascript. If you are going do be doing any sort of serious development, you can not rely on
printfstyle debugging, espeically since your browser probably has a javascript debugger already. If you ran your code through the debugger, you will be able to immediately see what caused the problem in your post. It might take you a while to learn to use the debugger effectively, but it will save you much more time down the road and help you write better code.BTW I recommend that you debug your code either in FireFox with Firebugs or Chrome since these two browsers offer good development tools.