This may not work for everyone, but I am working around this problem using dynamic loading.
In my development environment I use both Boostrap Studio (BSS), Eclipse and some deployment of the LAMP stack (e.g. WAMP) to provide the HTTP and database server back-end. Whilst BSS is really great for putting the UI together very quickly, it does not provide the most satisfying environment for editing more complex JavaScript files yet.
So, to keep the amount of JavaScript to be edited in BSS as small as possible and keep my application footprint small, I do the following.
- Download the Require.js API and import it into Bootstrap Studio (yes, you can also link it from CDN or your own HTTP server).
- Create a small JavaScript file in BSS to load the Require.js configurations (see xloader.js example).
- Create a simple dependency manager script to load the JavaScript file required for a specific page (see depman.js example).
- Put a page name in the ID attribute of the page BODY tag.
- Create the JavaScript file to be loaded for the page.
Some basic environment information:
- My development environment uses the BSS configurations and I have set it up to use jQuery 2.2.4 on the Design Settings -> Main tab.
- I do not use code round tripping, so all the assets created by BSS will be overwritten every time I do an export.
- To prevent cross-site (XSS) complexities I not use the BSS preview function. Instead I export all the code to the filesystem from where my HTTP server server them up and I can edit all the non-BSS files using Eclipse.
- <HOME> is a placeholder for the directory BSS is configured to export to.
- BSS exports the JavaScript files into a folder name <HOME>/assets/js folder. For simplicity I put all the custom JS files into the same structure. For this example, my custom application code is therefore in the <HOME>/assets/js/app folder.
The following file is an example of the xloader.js file previously referred to. This file will load the Require.js configurations and bring my custom dependency manager into scope.
require.config({
//By default load any module IDs from js/lib
baseUrl: 'assets/js',
//except, if the module ID starts with "app",
//load it from the js/app directory. paths
//config is relative to the baseUrl, and
//never includes a ".js" extension since
//the paths config could be for a directory.
paths: {
}
});
require(["app/depman"], function(depman) {
//alert("Loaded app/depman.js");
});
The following is an example of the <HOME>/assets/js/app/depman.js file. This piece of code looks at the content of ID attribute of the HTML file's BODY element, and loads a Required.js module JS file with the same name into scope.
define([], function() {
var pageName = document.body.id;
if(pageName == null || pageName == "") {
// Does nothing right now, but could be used to log errors.
} else {
require(["app/" + pageName], function(pageLogic) {
})
}
})
By way of an example, my index.html file has a BODY element with the ID attribute set to "index".
<HTML>
<BODY ID="index">
<!-- Lots of HTML stuff -->
</BODY>
</HTML>
The <HOME>/assets/js/app/depman.js file would therefore automatically load the Require.js module code contained in the <HOME>/assets/js/app/index.js file. Here is an example of the index.js file.
define([], function() {
alert("app/index.js file loaded");
// A lot more functions and variables and stuff here
})
Require.js is capable of a lot more, but hopefully this helps someone out.