如何从另一个需要“控制器”的控制器初始化控制器? [英] How can I initialize a controller from another controller that 'needs' it?
问题描述
CompaniesController
和一个 IndexController
。事实证明,所有的数据我的索引
路由需求来自 CompaniesController
。所以,我指定了我的 IndexController
,如下所示: IndexController = Ember.ArrayController.extend({
需要:'companies',
});
如果 CompaniesController
已经初始化,但是我第一次访问网站呢? CompaniesController
为空。
所以,我需要初始化 CompaniesController
从 IndexController
中。如何做到这一点?
使用 setupController
和 controllerFor
IndexRoute
:
App.IndexRoute = Ember.Route.extend({
model:function(){
return this.store.find('company');
},
setupController :function(controller,model){
this.controllerFor('companies')。set('model',model);
}
});
Say I have two controllers, a CompaniesController
and an IndexController
. It turns out that all the data my Index
route needs comes from the CompaniesController
. So, I've specified my IndexController
like this:
App.IndexController = Ember.ArrayController.extend({
needs: 'companies',
});
This works well if the CompaniesController
is already initialized, but what about the first time I visit the site? CompaniesController
is empty.
So, I need to initialize the data for CompaniesController
from within the IndexController
. How do I do this?
Use setupController
and controllerFor
within the IndexRoute
:
App.IndexRoute = Ember.Route.extend({
model: function() {
return this.store.find('company');
},
setupController: function(controller, model) {
this.controllerFor('companies').set('model', model);
}
});
这篇关于如何从另一个需要“控制器”的控制器初始化控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!