An activator is a special computed obseravble whose write function enforces the activation lifecycle when its value tries to change.
The Activation Lifecycle
canDeactivate
is called on the current valuecanActivate
is called on the value trying to be setdeactivate
is called on the current valueactivate
is called on the value trying to be set
Using an activator is done to force some object to fire off these events when its value changes, and block the change if canDeactivate
or canActivate
return false
.
It does not sound like you want need an activator. You aren't trying to enforce value changes happen according to these rules, you are trying to make sure a module performs some logic before it is used. It only needs to perform this logic once, as it is a singleton.
Unfortunately RequireJS does not provide a way (that I know of) to wait for a module to perform async work.
Here is one thing you could do. Since Durandal's app.start
initialization is an asynchronous process, you could hook into it so that your call to setRoot
comes after global
has initialized.
Global File
define(['durandal/app', 'plugins/ajax', 'durandal/global'], function (app,ajax) {
return function GlobalModule() {
activate: function() {
return //some promise;
}
};
});
Main.js
define(['durandal/system', 'durandal/app', 'durandal/global'],
function (system, app, global) {
app.configurePlugins({ /* plugins */ });
app.title = 'App';
app.start()
.then(global.activate)
.then(function () {
app.setRoot('shell');
});
});
This will ensure that setRoot
doesn't start until global
has finished activating. It should then be safe in any of your app code to use it.
Please keep in mind I have no idea what durandal/global
is, and this work depends on activate
being a promise returning function.