A simple, synchronous require framework for the Kony development platform. Provides dependency resolution and injection, filling a void for Kony.
While this technically works in all JS environments, most already have much more robust module frameworks established. If you find yourself stuck using Kony, this framework might help.
Include AA_require.js
in your Kony project under modules/js
. Since Kony loads and executes JavaScript files using an ascii value sort on filenames, the AA_
prefix helps ensure this file executes before any of your own module definitions try to register themselves. If you see errors along the lines of "define is not defined", you might need another A
on the file prefix :)
To define a module with no dependencies (note that the second parameter can also be any falsy value, such as null or an empty string):
define('ModuleName', [], function() {
// "the module"
var PUBLIC_API = {};
// private stuff
var answer = 42;
// public stuff
PUBLIC_API.foo = 'bar';
PUBLIC_API.getAnswer = function() {
return answer;
}
// return the module
return PUBLIC_API;
});
To define a module with one or more dependencies (note that for a single dependency, you don't need to wrap the module name in an array, but still can if you choose):
define('ModuleName', ['OtherModule', 'YetAnotherModule'], function(OtherModule, YetAnotherModule) {
var PUBLIC_API = {};
// you can use injected modules anywhere in this function scope
PUBLIC_API.answer = YetAnotherModule.getAnswer();
return PUBLIC_API;
});
In the rare case that you need to dynamically include a module in your code, use require(): (note that define() uses require() internally to fulfill dependencies)
var SomeModule = require('SomeModule');
This code creates two global variables, require
and define
.
Defining modules with circular dependencies is not supported.
If you NEED a circular dependency, ex. module A depends on module B, but module B also depends on module A, you can explicitly define A depending on B, then inside one of B's methods you can do
var A = require('A');
as long as that method isn't executed until after all modules are defined. Since this creates a "hidden" dependency, this pattern is discouraged. If you find you need a circular dependancy, it's best to first reconsider your architecture.