0

在我的单页应用程序(仿照 durandal.js、knockout.js 和 require.js 的 Hot Towel 模型)中,我试图设置一个标志 (HasAccess) 以指示用户是否已通过身份验证。我在一个名为 shell.js 的文件中执行此操作。该文件在每个视图中运行(index.vbhtml 引用 main.js,main.js 将应用程序根设置为 shell 视图,它在每个视图中运行 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

我遇到的问题是进入引导方法中的 else 语句,然后在 SecurityDataService.js 中运行对 getHasAccess 的调用(在激活 AccessDenied 视图之后)。

如何在确定要显示哪个视图之前先设置 HasAccess 标志(如果已通过身份验证,则为主页视图,如果未通过身份验证,则为 AccessDenied 视图)?

4

1 回答 1

0

getHasAccess正在进行 $.getJSON 调用,因此您可能需要通过 '$.when() to make sure thatgetHasAccess` 进行包装,然后再继续。

顺其自然。

function boot() {
  return  $.when(SecurityDataService.getHasAccess()).then(function(HasAccess){

        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);
        }
    };

}

顺便说一句,在他们var HasAccess = ko.observable(HasAccess);的顶部时刻shell.js被定义为可观察的,但在 boot() 中,它被作为普通变量处理HasAccess = SecurityDataService.getHasAccess();

于 2013-07-08T15:12:48.020 回答