.net Durandal 2.0 shell.js未在每个视图中运行

.net Durandal 2.0 shell.js未在每个视图中运行,.net,durandal,single-page-application,durandal-2.0,.net,Durandal,Single Page Application,Durandal 2.0,我将我的应用程序升级到Durandal 2.0。我的应用程序是以HotTower为模型的,带有main.js和shell.js。我认为shell.js在每个视图上都运行,但事实并非如此。我的shell.js包含我的安全代码,当然必须在每个视图上运行。如何强制shell.js在每个视图上运行 这里是main.js的摘录- define(['durandal/app', 'durandal/viewLocator', 'durandal/system', 'plugins/router', 'se

我将我的应用程序升级到Durandal 2.0。我的应用程序是以HotTower为模型的,带有main.js和shell.js。我认为shell.js在每个视图上都运行,但事实并非如此。我的shell.js包含我的安全代码,当然必须在每个视图上运行。如何强制shell.js在每个视图上运行

这里是main.js的摘录-

 define(['durandal/app', 'durandal/viewLocator', 'durandal/system', 'plugins/router', 'services/logger'],
function (app, viewLocator, system, router, logger) {

system.debug(true);

app.configurePlugins({
    router: true,
    dialog: true,
    widget: {
        kinds: ['expander']
    }
});

app.start().then(function () {
    toastr.options.positionClass = 'toast-bottom-right';
    toastr.options.backgroundpositionClass = 'toast-bottom-right';

    viewLocator.useConvention();
    router.makeRelative({ moduleId: 'viewmodels' });

    // Adapt to touch devices
    //Show the app by setting the root view model for our application.
    app.setRoot('viewmodels/shell');

    return router.map([
            { route: 'home', moduleId: 'home', title: 'home', title: 'Home', nav: true },
            { route: 'CAApproval', moduleId: 'CAApproval', title: 'CA Approval', nav: true }
    ]).activate();

});
摘自shell.js-

 define(['durandal/system', 'plugins/router', 'services/logger', 'services/SecurityDataService'],
function (system, router, logger, SecurityDataService) {

    var HasAccess = ko.observable();
    var test = ko.observable('it works');


    router.map('About', 'About', 'About', false);
    router.map('Help', 'Help', 'Help', false);

    var vm = {
        activate: activate,
        router: router,
        User: ko.observable(),
        showAbout: 'About',
        showHelp: 'Help',
        test: 'itworks'
    };
    return vm;

    function showAbout() {

       router.navigate('About'); // should show about view
    }

    function showAbout() {

       router.activate('Help'); // should show about view
    }

    function activate() {
        alert("here");
        $.when(
            $.ajax({
                url: '/api/security/CheckSecurity/',
                dataType: 'json',
                success: function( data ) {
                    strHasAccess = "";
                    if (typeof (data) == "string") {
                        strHasAccess = $.parseJSON(data);
                        HasAccess = strHasAccess[0].HasAccess;
                        vm.User = strHasAccess[0].UserName;
                        $('#spnUserName').text(vm.User);
                    } else {
                        HasAccess = false;
                    }

                    return strHasAccess;
                },
                error: function (data) {
                    amplify.store("ErrorDetails", data.responseText);
                    log('Error!', null, true);

                    //return router.activate('ErrorPage'); // should show details page of a particular folder

                }
            })
        ).then(function (HasAccess) {

            if (strHasAccess[0].HasAccess == true) {
                router.buildNavigationModel();
                vm.User = strHasAccess[0].UserName;
               router.navigate('home');

            }
            else {
                 router.map([
            { route: 'AccessDenied', moduleId: 'AccessDenied', title: 'AccessDenied', title: 'AccessDenied', nav: true }
                ]).activate

                 router.navigate('AccessDenied');
                log('Access Denied!', null, true);
            }
        });
    }

    function log(msg, data, showToast) {
        logger.log(msg, data, "shell.js", showToast);
    }
});

当您从一个视图导航到另一个视图时,Shell将被合成一次,视图将被注入Shell。在导航到视图之前检查某些条件的一种方法是设置警戒路线

app.start().then(function() {

    viewLocator.useConvention();

    app.setRoot('shell/shell', 'entrance');

    router.guardRoute = function (instance, instruction) {
        //auth check here
    };
});

另一种方法是在每个视图(模型)中使用canActivate()检查某些内容,并在不满足条件时将用户发送到其他位置。

我知道这不是您所要求的;但是jQuery.ajax方法不推荐使用
success
error
;您应该使用
done
fail
来代替。是的,使用
guardoote
绝对是进行安全类型检查的地方。不过,使用子路由器时要小心,
guardouste
方法不会从其父路由器复制到子路由器。