2013-10-18 14 views
0

所以我添加了一個方法來HomeView這樣的:EmberJS - 添加的方法來查看和調用它

App.HomeView = Ember.View.extend({ 
    isFailed: function() { 
     console.log(this); 
     return "FAILED" === this.get("state"); 
    } 
}); 

在HTML,這是家裏的模板的外觀。正如你可以看到我打電話一個循環內,並且由於某種原因isFailed功能,這是從來沒有所謂的(在日誌中沒有):

<script type="text/x-handlebars" id="home"> 
    <table> 
     <thead> 
     <tr> 
      <th>ID</th> 
      <th>Status</th> 
      <th>Foo</th> 
      <th>Bar</th> 
     </tr> 
     </thead> 

     <tbody> 

     {{#each}} 
     <tr> 
      <td>{{id}}</td> 
      <td> 
       {{#if isFailed }} 
       FAILED 
       {{/if}} 
      </td> 
      <td>bar</td> 
      <td>foobar</td> 
     </tr> 
     {{/each}} 

     </tbody> 
    </table> 
</script> 

任何想法,爲什麼沒有被稱爲isFailed功能?

的jsfiddle:http://jsfiddle.net/dWxTn/2/

回答

1

把手是logicless模板引擎,它不會永遠執行代碼。它只使用值並打印它們。

在這裏有可能isFailed總是會返回真理,因爲它是一個函數。

要使用它,使用Ember computed property

isFailed: function() { 
    console.log(this); 
    return "FAILED" === this.get("state"); 
}.property("state") 

這樣isFailed將是一個布爾每個state改變

+0

檢查出我的jsfiddle,我不能得到這個工作時間自動更新:HTTP: //jsfiddle.net/dWxTn/1/ –

+0

http://jsfiddle.net/dWxTn/2/ –

+0

讓它在這裏工作:http://jsfiddle.net/baPS6/事情是你有多個模型,所以你需要將計算的屬性附加到每個,而不是全局視圖。 –

相關問題