如何从另一个需要“控制器”的控制器初始化控制器? [英] How can I initialize a controller from another controller that 'needs' it?

查看:118
本文介绍了如何从另一个需要“控制器”的控制器初始化控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有两个控制器,一个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆