As I develop an app using MobileFirst v8 and Ionic v1.3.1, I encounter issues with timing in my code execution. Specifically, when the application initiates, the regular ionic angular code within my app.js
file runs.
This section of the code handles the initialization:
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
...
})
.controller('IndexCtrl', function($scope, $state) {
RememberMeService.checkIfLoggedIn().success(function(data) {
alert("YAY!!");
}).error(function(data) {
alert('fail :(');
});
});
function wlCommonInit() {
var serverUrl = WL.App.getServerUrl(function(success){
console.log(success);
}, function(fail){
console.log(fail);
});
WLAuthorizationManager.obtainAccessToken().then(
function (accessToken) {
console.log(">> Success - Connected to MobileFirst Server");
},
function (error) {
console.log(">> Failed to connect to MobileFirst Server");
console.log(error);
}
);
The following snippet represents the RememberMeService:
.service('RememberMeService', function($q) {
return {
checkIfLoggedIn: function() {
var deferred = $q.defer();
var promise = deferred.promise;
var securityCheckName = 'UserLogin';
WLAuthorizationManager.obtainAccessToken(securityCheckName).then(
function (accessToken) {
console.log('USER IS LOGGED IN!!!');
},
function (response) {
console.log("obtainAccessToken onFailure: " + JSON.stringify(response));
}
);
promise.success = function(fn) {
promise.then(fn);
return promise;
}
promise.error = function(fn) {
promise.then(null, fn);
return promise;
}
return promise;
}
}
})
An issue arises when the RememberMeService is invoked, resulting in a failure at the line where
WLAuthorizationManager.obtainAccessToken(securityCheckName).then
is called due to it stating that WLAuthorizationManager
is not defined. This occurs because the asynchronous function wlCommonInit()
from app.js
has not completed yet.
How can I implement a delay in either the controller or service being executed until wlCommonInit()
completes, and WLAuthorizationManager
is defined?
Your assistance is greatly appreciated.
EDIT
Below is the updated wlCommonInit()
function:
function wlCommonInit() {
var serverUrl = WL.App.getServerUrl(function(success){
console.log(success);
}, function(fail){
console.log(fail);
});
WLAuthorizationManager.obtainAccessToken().then(
function (accessToken) {
console.log(">> Success - Connected to MobileFirst Server");
angular.bootstrap(document.getElementById('indexBody'), ['app']);
},
function (error) {
console.log(">> Failed to connect to MobileFirst Server");
console.log(error);
}
);
Here is the corresponding segment from my index.html:
<body id="indexBody">
<h1>test</h1>
</body>
Upon implementation, an error message is displayed:
kError in Success callbackId: WLAuthorizationManagerPlugin887134242 : Error: [$injector:modulerr] Failed to instantiate module ng due to:
TypeError: Cannot set property 'aHrefSanitizationWhitelist' of null
Is there any flaw in how I am attempting to bootstrap the application?