How to get controller from component in latest Ember 2.3.0?

Hi Ember Developer ,

Previously I was using Ember views to access controller . For Ex

testController : Em.computed.alias(“parentView.controller”) like this …

In Latest Ember Version ember views concept deprecated despite supporting Ember-legacy-views .

So, now I am planning to migrate views to component . So now I cannot access my controller from component .

Please give suggestion … ???

As per the concept of components, they are completely isolated from the surrounding context, which means controllers will not be available. Even if you pass controller to the component like (never tried)

{{#my-component controller=this}}
...
{{/my-component}}

again you are making your component dependent on the controller which is against the principles of components and kills the purpose of reusability.

You should make your component self aware (meaning it knows how to handle its states, computes its required properties based on passed attributes etc) and make it totally independent and pass only required parameters from your controller to component.

The only reason you should pass the controller instance to a component is when your component needs to listen to some custom event(s) that’s triggered from the controller. Otherwise, if you need to communicate or send back something to the controller, use closure actions e.g. {{the-component someAction=(action 'someControllerAction')}} or {{the-component someAction=(action 'someControllerAction' someCurriedValue)}}

1 Like

Hi @vinothwindows47,

The question is… what kind of data are you accessing in your controllers?

Controllers don’t map 1:1 to components. They were also used to keep long-lived state around. Nowadays, Ember Services are used for this.

If you are migrating, it’s possible that these functions belong to a Service, not a Component (which are isolated).

There is some discussion about this topic in an article I wrote a few months ago: http://emberigniter.com/should-we-use-controllers-ember-2.0/

1 Like