2013-07-19 44 views
3

說我有兩個控制器,一個CompaniesController和一個IndexController。事實證明,我的Index路由需要的所有數據來自CompaniesController。所以,我指定我IndexController這樣的:如何從另一個需要控制器初始化控制器?

App.IndexController = Ember.ArrayController.extend({ 
    needs: 'companies', 
}); 

這種運作良好,如果CompaniesController已初始化,但對於我第一次訪問該網站? CompaniesController是空的。

因此,我需要從IndexController內初始化CompaniesController的數據。我該怎麼做呢?

回答

5

使用setupControllercontrollerForIndexRoute內:

App.IndexRoute = Ember.Route.extend({ 
    model: function() { 
    return this.store.find('company'); 
    }, 
    setupController: function(controller, model) { 
    this.controllerFor('companies').set('model', model); 
    } 
}); 
+0

我正要回答,對不起,因爲延遲...但我看到最好等待,現在你已經知道了你自己,這給了更多的自我估計:) – intuitivepixel

+1

你會想要移動/組'App.Company.find'放入'model'或'afterModel'掛鉤中,這樣它就會暫停加載數據。或者,如果您已經在這些鉤子中加載數據,則將其鏈接到另一個承諾。 –

+0

@DarshanSawardekar但是不是用於在當前路線上設置模型的'model'鉤子?我不會將公司數組設置爲「IndexController」的模型,對吧? –

1

這聽起來像你對我可能要逆轉的依賴,有你的CompaniesController依賴於應用程序,就像這樣:

App.CompaniesController = Ember.ArrayController.extend({ 
    needs: 'application', 
    contentBinding: 'controllers.application.companies' 
}); 

然後,只需在第一次加載基本路由時根據需要初始化您的應用程序。