Electron TypeError: $(...).jstree is not a function when i try to use it

Viewed 2626

i'm using jstree i have downloaded code from here https://www.jstree.com/

found jstree.js from dist folder and put into my /js/jstree.js

imported jstree like this:

if(inElectron()){
    window.jQuery = require('./js/jquery.min.js');
    window.$ = window.jQuery;
    window.jstree = require('./js/jstree.js');
}

when i try to use it:

$('#jstree').jstree();// for just demo with minimal code (Actually i'm using full code from here http://jsfiddle.net/t4s7dz52/)

i'm getting below error:

"TypeError: $(...).jstree is not a function

Note: i'm able to use jQuery throughout project ,so no import error as both js are in same folder

Please help me thanks in advance!!!!

3 Answers

The jstree module exports a factory which has to be invoked first, so that jstree can register as jQuery plugin. Try the following:

window.jQuery = require('./js/jquery.min.js');
window.$ = window.jQuery;
window.jstree = require('./js/jstree.js')(window.$);

If using electron:

<script>window.$ = window.jQuery = require('./path/to/jquery');</script>

Have you tried this:

//wrapped to load after jQuery
$(function(){
    ....
    window.jstree = require('./js/jstree.js');
    $('#jstree').jstree();
    ....

});
Related