AuthService.js:
SettingService.loadSettings().then(function(config){
console.log('loading settings')
settings = config;
expirationDate = moment(settings.trialEndDate);
remainingDays = expirationDate.diff(moment(),'days');
isExpired = remainingDays <= 0 ? true : false;
});
然后在我的app.js中我有一些UI-Router事件处理程序来检查试用版或许可证是否有效:
app.js:
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
AuthService.hasAccess().then(function(result){
console.log('access result',result)
if ( (hasAccess == false) && toState.name !== "settings") {
event.preventDefault();
toastr.warning('Your trial has expired');
$state.go('settings');
}else{
console.log("License is valid or trial not expired.");
}
});
});
AuthService.js&gt; hasAccess():
function hasAccess() {
var d = $q.defer();
// test if they have license
console.log('Check if license present');
if (_.isNull(settings.organization.license)){
// No License
// Return trial status
d.resolve(isExpired);
}else{
// There IS a license
// Validate license
licenseCheck().then(function(licenseResult) {
d.resolve(licenseResult);
})
}
return d.promise
}
问题:在出厂设置和过期状态之前,我的功能(hasAccess()
)检查我的许可证是否有效。 settings
正在返回undefined
。是否可以继续致电hasAccess()
,直到settings
不再定义为止?