2014-02-05 48 views
0

是否可以在一個模板中使用{{action}另一個模板中的{{bind-attr}}。Ember.js - 是否可以在一個模板中使用{{action}}來影響另一個模板中的{{bind-attr}}

目標是在1模板中獲取操作以更改另一個模板中的bind-attr。兩者有不同的控制器。

實施例:

模板1:

<script type="text/x-handlebars" data-template-name="diary"> 
    <header class="dashboard-component-header" {{ action expand }}></header> 
</script> 

模板2

<script type="text/x-handlebars" data-template-name="diary-section"> 
    <section class="dashboard-component-section" {{ bind-attr class="state:active" }}></section> 
</section> 

控制器模板1

App.DiaryController = Ember.Controller.extend({ 
    actions: { 
     expand: function() { 
      this.toggleProperty('state'); 
     } 
    } 
}); 
+0

訪問第二個控制器的實例是否只有一個模板/控制器2實例? – claptimes

+0

一個簡單的方法是在應用範圍內定義'狀態'。所以像App.state = false;並在你的控制器中,你基本上會切換App.state。 –

回答

0

我想你可以使用「需要」鉤在第一控制器來訪問第二控制器

http://emberjs.com/guides/controllers/dependencies-between-controllers/

,然後上的動作使用第二控制器的對象來設置,其使用綁定綁定的屬性-attr

App.DiaryController = Ember.Controller.extend({ 
    needs: "diarySection", 
    actions: { 
    expand: function() { 
     controllers.diarySection.set('state', false); 
    } 
    } 
}); 

其中 「diarySection」 對應App.DiarySectionCont滾動控制器模板2 ,您可以使用controllers.diarySection

+0

不幸的是,似乎沒有工作。 – Chris

相關問題