When using BajaScript version 1 in Niagara AX, the following script tag was required for BajaScript to work:
<script type="text/javascript" src="module://bajaScript/com/tridium/bajascript/bs.min.js"></script>
The more complex the web application, the more script tags are needed. This can have some negative effects on your web application:
As more developers transition their UI from Java to HTML5, we need a modular way to manage JavaScript. To solve this problem, we use RequireJS from https://requirejs.org. RequireJS uses the Asynchronous Module Definition (AMD) method of loading JavaScript code.
RequireJS has a wide variety of configuration options, but in most cases, Niagara will completely handle the configuration of it for you. The most important thing to learn about using RequireJS in Niagara is the AMD syntax itself.
// to define an AMD module:
// myModule-ux/src/rc/MyWidget.js
define([
'baja!',
'bajaux/Widget' ], function (
baja,
Widget) {
return class MyWidget extends Widget {
};
});
// to consume an AMD module:
define([
'nmodule/myModule/rc/MyWidget' ], function (
MyWidget) {
// write code that uses MyWidget here
});
baja! to obtain a running instance of BajaScript.nmodule prefix to retrieve a resource from any module. For instance, if your file is in myModule-ux/src/rc/MyWidget.js, the RequireJS ID to request is nmodule/myModule/rc/MyWidget.css plugin to make your bajaux widget require a CSS file. For instance, if your CSS file is at myModule-ux/src/rc/myModuleStyle.css, the RequireJS ID to request is css!nmodule/myModule/rc/myModuleStyle.lex plugin to request lexicon data. See the documentation here.log plugin to log data to the browser console, or send it up to the station log. See the documentation here.In addition to loading code in the browser at runtime, RequireJS allows multiple JS modules to be bundled up into single files. This improves caching and load times, and reduces pressure on a station’s web server.
Please see the section on Building JavaScript Applications for details.
Depending on how you are developing a web application, your use of RequireJS will vary slightly.
HxOp#requireJs() from your Hx View.$util.requirejs() to inject the RequireJS script tag and configuration object.<script src="/requirejs/config.js"></script> (preloads the RequireJS configuration)<script src="/module/js/com/tridium/js/ext/require/require.js"></script> (loads RequireJS itself)