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

Let's say I have two controllers: CompaniesController and IndexController . It turns out that all the data needed for my Index route comes from CompaniesController . So, I indicated my IndexController as follows:

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

This works well if CompaniesController already initialized, but what about the first visit to the site? CompaniesController empty.

So, I need to initialize the data for CompaniesController from IndexController . How to do it?

+3
source share
2 answers

Use setupController and controllerFor in IndexRoute :

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

It sounds to me as if you can change the dependency and make your CompanyController depend on the application, for example:

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

Then simply initialize the application if necessary when it first loads the base route.

+1
source

Source: https://habr.com/ru/post/949829/


All Articles