如何在我的单页应用程序中设置标志

时间:2013-07-08 14:46:24

标签: jquery asp.net-mvc-4 knockout.js single-page-application durandal

在我的单页应用程序(以热毛巾为模型的durandal.js,knockout.js和require.js)中,我试图设置一个标志(HasAccess)来指示用户是否已经过身份验证。我在一个名为shell.js的文件中这样做。该文件在每个视图中运行(index.vbhtml引用main.js,main.js将app root设置为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的dataservice文件中。该代码是 -

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视图之后)。

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

1 个答案:

答案 0 :(得分:0)

getHasAccess正在进行$ .getJSON调用,因此您可能需要在继续之前通过'$ .when()to make sure that getHasAccess`解压缩。

沿线的东西。

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

}

BTW在var HasAccess = ko.observable(HasAccess);顶部的shell.js被定义为可观察的,但在boot()中它被作为正常变量处理HasAccess = SecurityDataService.getHasAccess();