additional js library

2
We need to add some aditional js library to our mendix application. The only way to do this is by adding the js reference into the index.html. Because this library is really huge I wouldn't prefer to load this library in the theming. This is because the library is only needed in combination with a particular widget. Is there some way to include js libraries in your widget? Otherwise we have to load this js library each time. And will the js file be cached and/or gezipped?
asked
3 answers
3

Best is to use the dojo loading mechanism. In top of the .js file of your library add:

dojo.provide("path.to.my.library");

and in the widget add on the top (or in the postCreate for lazy loading)

dojo.require("path.to.my.library");

The advantage of this approach is (in contrast to Roelands approach) that dojo will make sure that the library is loaded before your widget, so that the widget can refer safely to it. Furthermore dojo ensures that the library will be load only once, even if multiple widgets refer to it. Simple chart provides indeed an example of this approach.

answered
1
var myModuleLoad = mendix.dom.script({"src" : mx.moduleUrl("MyModule",'js/additional.js'), "id" : 'MyScript'});
document.getElementsByTagName("head")[0].appendChild(myModuleLoad);

this is wat I've used in javascript.

I dont know if it will be cached.

answered
1

I also found a way to apend a javascript library in the widget. You could take a look at the simplechart widget in the appstore.

answered