2013-07-12 44 views
0

我有以下看法:從一個不相關的控制器更改視圖屬性

App.MessageTrayView = Bootstrap.AlertMessage.extend({ 
    message: 'This is a message.', 
}); 

在此模板顯示:

<script type="text/x-handlebars" data-template-name="nodes"> 
    <article> 
     <form class="form-horizontal"> 
     <fieldset> 

      {{view App.MessageTrayView id="message-tray-view"}} 

      <div id="legend" class=""> 
      <legend class="">Nodes&nbsp;&nbsp;&nbsp;<span class="badge">{{controllers.nodesIndex.length}} records</span> 
      <div class="pull-right"> 
       <a {{action destroyAllRecords}}><i class="icon-remove-circle"></i><a/> 
       {{#linkTo "nodes.new" class="btn btn-primary"}}Add Node{{/linkTo}} 
      </div> 
      </legend> 
      </div> 

      {{outlet}} 

     </fieldset> 
     </form> 
    </article> 
    </script> 

這無關控制器:

App.NodesIndexController = Ember.ArrayController.extend({ 
    destroyAllRecords: function() { 
     console.log('destroyAllRecords called'); 
     App.MessageTrayView.set('message', 'All nodes have been deleted'); 
    }, 
}); 

我想在destroyAllRecords被觸發後立即更改顯示的消息。這不起作用(控制檯中的錯誤消息告訴我,我正在做一些*非常*錯誤的事情)。如何更改message屬性,以便更改在頁面上直接顯示?

你可以看到代碼直播here

回答

1

這樣做可能是對App命名空間定義屬性的一種快捷方法:

App = Ember.Application.create({ 
    messageTrayContent: '' 
}); 

然後使用後綴Binding綁定到它在你的觀點您的屬性名後:

App.MessageTrayView = Bootstrap.AlertMessage.extend({ 
    messageBinding: 'App.messageTrayContent' 
}); 

現在做的:

App.NodesIndexController = Ember.ArrayController.extend({ 
    destroyAllRecords: function() { 
    console.log('destroyAllRecords called'); 
    App.set('messageTrayContent', 'All nodes have been deleted'); 
    }, 
}); 

應該工作。

希望它有幫助。

+0

謝謝,這是有效的。我曾經看過在視圖中定義綁定到應用程序屬性的示例,但我希望有一種直接的編程方法來修改視圖屬性。 – dangonfast

+0

@gonvaled,是的,我瞭解你,事情是,因爲視圖的意圖是鬆散耦合的綁定機制是首選的方式 – intuitivepixel

相關問題