2013-07-19 43 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' 
}); 

然后,只需在第一次加载基本路由时根据需要初始化您的应用程序。