Durandal 2.0 shell.js没有在每个视图中运行

时间:2013-12-17 13:47:22

标签: .net durandal single-page-application durandal-2.0

我将我的应用程序升级到Durandal 2.0。我的应用程序是在HotTowel之后建模的,带有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);
    }
});

1 个答案:

答案 0 :(得分:3)

Shell组成一次,当您从一个视图导航到另一个视图时,视图将注入到shell中。在导航到视图之前检查某些条件的一种方法是使用保护路径。

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

    viewLocator.useConvention();

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

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

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