2014-11-05 70 views
1

我在控制器上有一個屬性,我們將其稱爲showTheStuff,並且我的模板中有一個{{#each}}幫助器。現在,我只是明確地設置屬性。Ember:從每個幫手中訪問控制器屬性

在該列表中,我想根據showTheStuff的值有條件地呈現或不呈現某些標記,但如果使用{{#unless}}條件,則不起作用。

http://emberjs.jsbin.com/jarapabela/1/

我如何得到這個工作? 在我的代碼中,它不能與{{#unless}}一起使用,但如果我切換屬性並使用{{#if}},它確實有效。有沒有其他人經歷過這個?

我正在使用Ember 1.6。

控制器:

export default Ember.ArrayController.extend({ 
    hideDeleteButton: true, 
    actions: { 
    deleteComment: function(comment) { 
     comment.destroyRecord(); 
     this.removeObject(comment); 
    } 
    } 
}); 

模板: 模板是另一個模板內呈現的模態分量。打開它的{{#link-to}}通過傳遞一組註釋。該屬性在{{#each}}之外生效,但它內部必須是未定義的,因爲即使嘗試在第一個單元格中輸出它,渲染時也不會顯示任何內容。

<table class="table table-striped table-condensed"> 
    <thead> 
     <tr> 
     <th>Comments</th> 
     {{#unless hideDeleteButton}} 
      <th></th> 
     {{/unless}} 
     </tr> 
    </thead> 
    <tbody> 
     {{#each}} 
     <tr> 
      <td> 
      {{hideDeleteButton}} 
      <br> 
      {{comment}} 
      </td> 
      <td> 
      {{createdByResource}}{{hideDeleteButton}} 
      </td> 

      {{#unless hideDeleteButton}} 
      <td class="text-center"> 
       {{confirm-delete-button action="deleteComment" param=this}} 
      </td> 
      {{/unless}} 
     </tr> 
     {{/each}} 
    </tbody> 
    </table> 
+0

似乎用,除非工作。想想,除非像一個不是。你可以嘗試並多解釋一下你在找什麼? – blackmind 2014-11-05 23:20:08

+0

它在bin中工作,我無法弄清楚爲什麼在這個世界裏它不會在我的代碼中工作,這非常簡單。如果我使用If助手,一切正常。如果我只是將「如果」更改爲「除非」,然後切換我的財產的平價,突然它不起作用。我想也許有一些錯誤,除非在每個幫手或其他內部。 – redOctober13 2014-11-05 23:35:56

+0

看起來不像一個燼蟲。我改爲v1.6,它仍然運作(它使用1.8)。你可以添加你的程序中的代碼,看看是否有區別 – blackmind 2014-11-05 23:40:14

回答

2

@ Kingpin2k導致我在正確的方向,我想jsbin沒有完全符合我的實際代碼。問題是我的hideDeleteButton屬性實際上並不知道每個屬性,因爲它遍歷了傳入的模型。

但是,如果我在指定屬性時指定了controller,它就起作用!

{{#unless controller.hideDeleteButton}} 
    <td class="text-center col-md-1"> 
    {confirm-delete-button action="deleteComment" param=this}} 
    </td> 
{{/unless}} 
+0

'控制器'現在已被棄用。任何其他方式來做到這一點? – 2015-08-21 11:11:26

+0

我只是仍然在Ember 1.11上,所以我不確定它在組件模型中如何工作。 – redOctober13 2015-08-31 14:25:46

1

確保您爲{{#each}}塊提供了一些上下文。

例如:{{#each對象模型}}或新的語法:{{#each模型|對象|}}

這應該允許您引用hideDeleteButton不使用 「controller.hideDeleteButton」

我假設hideDeleteButton是你的控制器和休息上的屬性對你的模特屬性:

{{#each model as |object|}} 
    <tr> 
    <td> 
     {{hideDeleteButton}} 
     <br> 
     {{object.comment}} 
    </td> 
    <td> 
     {{object.createdByResource}}{{hideDeleteButton}} 
    </td> 

    {{#unless hideDeleteButton}} 
     <td class="text-center"> 
     {{confirm-delete-button action="deleteComment" param=object}} 
     </td> 
    {{/unless}} 
    </tr> 
{{/each}} 
相關問題