2013-01-06 18 views
5

該代碼

我正在使用版本4fcdbf2560與新的路由器。如何使用新路由器重新呈現應用程序模板?

在我的應用程序中,用戶可以進行身份​​驗證,也可以不進行身份驗證。根據身份驗證狀態,呈現的模板不會相同。

我已經在ApplicationRoute重新定義函數renderTemplate管理此:

App.ApplicationRoute = Ember.Route.extend({ 
    renderTemplate: function() { 
     this.render(App.authenticated() ? 'authenticated' : 'unauthenticated'); 
    } 
}); 

我的路由器是相當簡單:

App.Router.map(function(match) { 
    match('/').to('index'); 

    match('/sign').to('sign', function(match) { 
     match('/in').to('signIn'); 
    }); 

    match('/dashboard').to('dashboard'); 
}); 

IndexRoute只是這裏的用戶視重定向驗證狀態:

App.IndexRoute = Ember.Route.extend({ 
    redirect: function() { 
     this.transitionTo(App.authenticated() ? 'dashboard' : 'signIn'); 
    } 
}); 

工作流

  1. 用戶導航到/
  2. ApplicationRoute輸入,作爲用戶沒有被認證,則unauthenticated模板呈現
  3. IndexRoute輸入,作爲用戶未被認證,一個重定向到signIn
  4. signIn模板被渲染成其父模板製作 - >的unauthenticated模板
  5. 將U SER登錄成功,route.transitionTo('dashboard')
  6. dashboard模板被渲染成其父模板 - >的unauthenticated模板

的問題

  • 這有什麼錯我的執行?
  • 爲什麼在dashboard模板呈現時renderTemplate函數未被調用?
  • 如何強制應用程序重新呈現其模板?

編輯2013年1月7日

我根據埃文的回答修改我的代碼。

我的應用程序模板,現在看起來是這樣的:

{{#if isAuthenticated}} 
    <h1>Authenticated</h1> 
    {{outlet}} 
{{else}} 
    <h1>Unauthenticated</h1> 
    {{outlet}} 
{{/if}} 

當應用程序頁面上的用戶的土地,因爲他沒有通過認證,這是它呈現的未經驗證的塊。除了沒有任何東西渲染成{{outlet}}標籤,一切都運行良好...

但是,當我的應用程序模板看起來像這樣(=沒有條件標籤):

<h1>Unauthenticated</h1> 
{{outlet}} 

...它的作品!所以我想知道是否可以在條件標籤之間插入{{outlet}}標籤。

+2

的內部塊插座:https://github.com/emberjs/ember.js/pull/1671,希望對你這個將被合併;) –

回答

2

我認爲在路由器中使用這種邏輯可能是錯誤的;相反,這應該是ApplicationController的一部分。

因爲作爲應用程序的狀態變化,你可以創建一個跟蹤驗證狀態

App.ApplicationController = Ember.Controller.extend({ 
    isAuthenticated: null 
}); 

的ApplicationController中,構建您的模板,這樣的餘燼會自動更新模板:

<script type="text/x-handlebars" data-template-name="application"> 
    {{ #if isAuthenticated }} 
     You are now logged in 
    {{ else }} 
     Please Log In 
    {{ /if }} 
</script> 

現在你不實際上不用擔心手動更新/渲染模板。隨着內部(JS)狀態的更改,您的模板將自動更新以反映應用程序狀態。

+0

感謝您的回答。我編輯了我的問題以添加更多信息。 – Victor

+0

我已經接受了你的答案,因爲這是一個好的方法;) – Victor

相關問題