2014-09-04 43 views
0

我讀過Durandal login page redirect pattern哇,很多代碼可以做我認爲很簡單的代碼。如何使用與Durandal具有不同佈局的單獨登錄頁面?

我也通讀了https://groups.google.com/forum/#!topic/durandaljs/RdGpwIm1oOU,因爲我希望登錄頁面有一個帶有登錄表單的簡單徽標,但我也想要路由註冊和關於頁面。我的網站的其餘部分將有一個菜單,標題等,我不想顯示,直到用戶登錄。此外,我不知道當用戶登錄時如何更新此方法。

另一個代碼示例,幾乎做我想做的事情:https://github.com/Useful-Software-Solutions-Ltd/Durandal451/blob/master/Durandal451v2/App/global/session.js

那麼,我該怎麼做?有沒有官方的方式來做到這一點?人們嘗試過的東西似乎有些微不足道。我認爲這將是一個很常見的情況,但在主文檔中找不到任何東西。

回答

0

我不知道這是最簡單的方法,但是這是我得到

你需要app.start後添加一些額外的功能()被觸發。

main.js

var auth = require('authentication'); // Authentication module 

app.start().then(function() 
{ 
    // This function will wait for the promise 
    auth.init().then(function(data) 
    { 
     // When successfully authenticate, set the root to shell 
     app.setRoot('views/shell'); 
    } 
}); 

authentication.js

define(function(require) 
{ 
    var app = require('durandal/app'); 

    return { 
    init: function() 
    { 
     // Initialize authentication... 

     return system.defer(function(dfd) 
     { 
      // Check if user is authenticate or if there has stored token 
      var isAuthenticate = someOtherFunctiontoCheck(); 

      if (isAuthenticate) 
      { 
       dfd.resolve(true); // return promise 
      } 
      else 
      { 
       // When not authenticate, set root to login page 
       app.setRoot('views/login'); 
      } 
     } 
    } 
    }; 
}); 

好運! :)

UPDATE

login.js

​​

Authentication.js

define(function(require) 
{ 
    var app = require('durandal/app'); 

    return { 
    init: function() 
    { 
     // Initialize authentication... 

     return system.defer(function(dfd) 
     { 
      // Check if user is authenticate or if there has stored token 
      var isAuthenticate = someOtherFunctiontoCheck(); 

      if (isAuthenticate) 
      { 
       dfd.resolve(true); // return promise 
      } 
      else 
      { 
       // When not authenticate, set root to login page 
       app.setRoot('views/login'); 
      } 
     } 
    }, 
    login: function(username, password) 
    { 
     // do authenticate for login credentials (e.g for retrieve auth token) 
     return $.ajax({ 
      url : 'api/login', 
      type : 'POST', 
      data : { 
       username: username, 
       password: password 
      } 
     }).then(function(token){ 
      // on success, stored token and set root to shell 
      functionToStoreToken(token); 

      // Set root to shell 
      app.setRoot('views/shell'); 
     }); 
    } 
    }; 
}); 
+0

什麼是您登錄樣子?我會認爲這是另一個殼?你有沒有單獨的路由之前和之後(這實際上很酷)?這只是在成功登錄時如何處理更新以切換根目錄?否則你的例子看起來很棒,根本不復雜。 – rball 2014-09-05 15:29:48

+0

答案是肯定的,你可以看到更新的答案。 :) – 2014-09-08 07:28:20

相關問題