2013-12-17 20 views
1

我將應用程序升級到了Durandal 2.0。我的應用程序在HotTowel之後建模,具有main.js和shell.js。我認爲shell.js運行在每個視圖上,但事實並非如此。我的shell.js包含我的安全代碼,這當然必須在每個視圖上運行。我如何強制shell.js在每個視圖上運行?Durandal 2.0 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); 
    } 
}); 
+0

我知道這不是你問的;但jQuery.ajax方法不推薦使用'success'和'error';你應該使用'done'和'fail'來代替。 – gerrod

回答

3

殼由一次和視圖被注入殼爲從一個導航查看另一個。在導航到視圖之前檢查某種情況的一種方法是有警戒路線。

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

    viewLocator.useConvention(); 

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

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

另一種方法可能是有canActivate()在每個視圖(模型)來檢查一番,和其他地方發送用戶當條件不滿足。

+0

是的,使用'guardRoute'確實是進行安全類型檢查的地方。儘管使用子路由器時要小心 - 「guardRoute」方法不會從其父節點複製到子路由器。 – gerrod