2013-01-22 64 views
1

可能重複:
Accessing controllers from other controllersEmber.js PRE4路由器獲取其他控制器

在餘燼PRE2和之前與舊路由器的風格,你可以從路由器獲得其他控制器,所以如果我在一個叫做PeopleController的控制器中,我可以做類似這樣的事情

App.PeopleController = Ember.Controller.extend({ 
    some_computed_property: (function() { 
      return this.get('target.otherController.property_i_want'); 
    }).property('target.otherController.property_i_want') 
}); 

或來自調試控制檯

> App.router.get('otherController.property_i_want') 

這兩個工作。 Pre4 /新的路由風格似乎打破了這一點。我如何通過新的路由器和pre4獲得此功能?

回答

3

我有一個可怕的黑客:

Em.Route.reopen({init:function(){ 
    window.App.currentRoute = this; 
    this._super.apply(this,arguments); 
}}) 

它可以讓你做這樣的事情:

App.currentRoute.controllerFor('something'); 
App.currentRoute.target... 

編輯: 目前,燼支持定義 「需要」 的控制器,以及將容器暴露給查找:

App.__container__.lookup("controller:application").get("someProperty") 

App.ApplicationController = Em.Controller.extend({ 
    needs: ["authentication","notifications"], 
    init: function(){ 
     this._super.apply(this,arguments) 
     console.log(this.get("controllers.authentication"), this.get("controllers.notifications")) 
    } 
}) 
+1

這很聰明,我喜歡那:) – wmarbut

2

從控制檯訪問的控制,在代碼中設置debugger;,刷新瀏覽器,將停止執行你設置debugger聲明,那麼你可以你在範圍內的訪問控制器,採用

this.controllerFor('abs'); 

這也調試模板非常有用,你可以插入{{debugger;}},並且允許您訪問模板的整個範圍在控制檯中,嘗試例如找出你controller或您的view的。