2
votes

How can I access ember component's controller from another controller? Is there a backdoor way like __container__.lookup just to know that there is this instance of component? When I tried __container__.lookup with "component:<myComponentname>" it gave me an instance of component which is not being used anywhere in the application.

TL;DR
How to test Ember Component objects?

1
Dirty hack which I'm following as of now... is I'm getting the id of the div containing the container and accessing its instance from Ember.View.views["ember_id_"]G Sree Teja Simha
So you basically want to access a controller from another controller? Is that right?mavilein
A specific component instance's controller not any other normal controller.G Sree Teja Simha

1 Answers

2
votes

These lines of ember-inspector reveal how to get a "view registry," a mapping of DOM element ids to their respective Ember.Component instance in Ember 2.x, or Ember.View instance in 1.x.

viewRegistry: computed('application', function() {
  return this.get('application.__container__').lookup('-view-registry:main') || View.views;
}),

So if you have an Ember component in your DOM like

<span id="ember1234">Hi I'm controlled by Ember</span>

you can look it up like

var viewRegistry = application.__container__.lookup('-view-registry:main') || View.views;
var myComponentInstance = viewRegistry.ember1234;