2014-10-10 95 views
0

我想在登錄後顯示用戶的用戶配置文件。所以我想我會使Application.Controller成爲一個ObjectController並將其模型屬性設置爲用戶對象。 我現在遇到的問題是我不知道如何在嵌套的配置文件模板中顯示用戶屬性? 我實際上看到了使用{{#each todo in App.todosController.content}}的其他答案。但只適用於ArrayController。也許這隻能用一個ArrayController,我真的不知道,但那會很奇怪。謝謝你的幫助!在Ember的嵌套模板中顯示ApplicationController的模型

爲了測試這個,我在登錄成功時將Application.Controller的模型設置爲用戶對象。 這裏是我的代碼:

<script type="text/x-handlebars" id="profile"> 
     <div class="content"> 
      <div class="content-padded"> 
       <div> 
        <p><h3>Username: {{{user.username}}}</h3><p> 
        <p>trials: {{user.trials}}</p> 
        <p>results: {{user.results}}</p> 
        <p>email: {{user.email}}</p> 
       </div> 
      </div> 
     </div> 
</script> 

App.Router.map(function() { 
this.resource('signup'); 
this.resource('login'); 
this.resource('profile'); 
this.resource('practice'); 
this.resource('overview'); 
}); 

App.LoginController = Ember.ObjectController.extend({ 
    model: {}, 
    needs: ['application'], 
    loggedInUser: {id: 9, 
    username: "rooty", 
    trials: "fghsds", 
    results: "fdfsd", 
    email: "[email protected]"}, 
    loginHandler: function(data) { 
     // sign in logic 
     this.set("controllers.application.isLoggedIn", true); 
     this.set("controllers.application.model", loggedInUser); 
    } 
}); 

App.ApplicationController = Ember.ObjectController.extend({ 
    isLoggedIn: false 
}); 

用戶對象是這樣的:

User {id: 9, 
     username: "rooty", 
     trials: "fghsds", 
     results: "fdfsd", 
     email: "[email protected]"} 

回答

0

你是如此接近!所以你的登錄控制器在應用程序控制器上設置模型。您的控制器的登錄控制器的needs屬性表示應用程序控制器範圍內的所有屬性都將可用於您的登錄控制器。遵循這個邏輯,你的配置文件控制器還不能needs: ['application']

隨着一點點的工作,我們可以稱之爲是指用戶喜歡你,從你的個人資料控制器喜歡:

needs: ['application'], 
users: Ember.computed.alias('controllers.application.model') 

然後在你的配置文件模板,你已經擁有的應該工作:

<div> 
    <p><h3>Username: {{{user.username}}}</h3><p> 
    <p>trials: {{user.trials}}</p> 
    <p>results: {{user.results}}</p> 
    <p>email: {{user.email}}</p> 
</div> 

JSBin

注意在JSBin中,在索引控制器上,我有一個計算應用程序模型的別名。索引模板中的輸入助手的值綁定到此計算的別名,即應用程序的模型。在應用模板中觀察,我只使用{{model}}。在輸入內容時,由於Ember的強大綁定,您可以看到模型更改,所以我們通過needs和計算的別名確定了應用程序模型的目標。

與配置文件模板相同的東西。

+0

Tanx sunrize920!這就像一個魅力。需要: ['application']我自己想通了,然而這個使用 Ember.computed.alias('controllers.application.model')對我來說是新的.. 我顯然沒有充分利用的功能。所以我很高興學習這一切! – sunchild 2014-10-11 16:16:01