私のシングル ページ アプリケーション (durandal.js、knockout.js、require.js のホット タオルをモデルにしたもの) で、ユーザーが認証されているかどうかを示すフラグ (HasAccess) を設定しようとしています。shell.js というファイルでこれを行っています。そのファイルはすべてのビューで実行されます (index.vbhtml は main.js を参照し、main.js はアプリ ルートをシェル ビューとして設定し、すべてのビューで shell.js を実行します)。shell.js ビューモデル コードは次のとおりです。
define(['durandal/system', 'durandal/plugins/router', 'services/logger', 'services/SecurityDataService'],
function (system, router, logger, SecurityDataService) {
var HasAccess = ko.observable(HasAccess);
var vm = {
activate: activate,
router: router
};
return vm;
function UserHasAccess() {
return SecurityDataService.getHasAccess(HasAccess);
}
function activate() {
return boot();
}
function boot() {
HasAccess = SecurityDataService.getHasAccess();
if HasAccess == "True") {
router.mapNav('home');
router.mapNav('CAApproval');
log('LucasNet Loaded!', null, true);
return router.activate('home');
}
else {
router.map([
{ url: 'AccessDenied', moduleId: 'viewmodels/AccessDenied', name: 'AccessDenied', visible: false }
]);
return router.activate('AccessDenied'); // should show details page of a particular folder
log('Access Denied!', null, true);
}
}
function log(msg, data, showToast) {
logger.log(msg, data, system.getModuleId(shell), showToast);
}
});
getHasAccess を含むファイルは、SecurityDataService.js というデータ サービス ファイルにあります。そのコードは-
define(['services/logger', 'durandal/system'],
function (logger, system) {
var SecurityModel = function (HasAccess) {
var self = this;
self.HasAccess = ko.observable(HasAccess);
};
var getHasAccess = function (strHasAccess) {
$.getJSON('/api/security', function (data) {
strHasAccess = "";
if (typeof (data) == "string") {
strHasAccess = data;
} else {
strHasAccess = "False";
}
return strHasAccess;
});
}
var dataservice = {
getHasAccess: getHasAccess
};
return dataservice;
});
編集してコントローラーコードを追加しました
Public Class SecurityController
Inherits ApiController
' GET api/security
Public Function GetValues()
Dim boolHasAccess As String = ""
Try
Dim objUser As LucasEntities.Business.IEF_WUserID = New LucasEntities.Business.EF_WUserID
Dim objSecurity As New LucasEntities.Business.EF_Security
boolHasAccess = objSecurity.GetUserPermissionsJSON(Utilities.GetLogin(), "CAAPPRV")
Catch ex As Exception
Dim shouldRethrow As Boolean = ExceptionPolicy.HandleException(ex, "Policy")
If shouldRethrow Then
Throw
End If
End Try
Return boolHasAccess
End Function
End Class
Return boolHasAccess
End Function
私が抱えている問題は、boot メソッドの else ステートメントに入り、SecurityDataService.js で getHasAccess への呼び出しを実行することです (AccessDenied ビューがアクティブ化された後)。
表示するビュー (認証されている場合はホーム ビュー、認証されていない場合は AccessDenied ビュー) を決定する前に、最初に HasAccess フラグを設定するにはどうすればよいですか?