2017-05-10 36 views
0

在ASP.Net MVC項目中使用durandal 2.1.0。 當視圖訪問我使用canActivate函數來表示它們是否可以訪問視圖,像這樣:canActivate期間的Durandal重定向

self.canActivate = function() { 
     return http.ajaxRequest("get", "/api/route/hasaccesstoroute?route=viewname") 
      .done(function (result) { 
       if (!result) { 
        app.showMessage("You do not have permissions to access this area!"); 
       } 
      }); 
    }; 

這種運作良好,但我需要一個視圖做的是重定向到另外的看法,有麻煩這樣做。任何人都可以幫助如何相應地改變?我試着navigateTo但說這不是一個有效的功能。

回答

2

我想通了這一點,並已離開的問題上應該比誰需要這樣做:

return http.ajaxRequest("get", "/api/route/hasaccesstoroute?route=viewname").then(function (response) { 
      if (response == true) { 
       return true; 
      } 
      else { 
       return { redirect: "#/viewname" }; 
      } 
     });