Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I initialize a controller from another controller that 'needs' it?

Tags:

ember.js

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?

like image 975
Sam Selikoff Avatar asked Aug 12 '26 07:08

Sam Selikoff


1 Answers

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);
  }
});
like image 129
Sam Selikoff Avatar answered Aug 15 '26 00:08

Sam Selikoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!