Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing an instance of a controller or a view in ember

My understanding is that when I run

App.CheeseController = Ember.Controller.extend({ type:"brie"});

A class CheeseController is created and that when I activate the Cheese route an instance of that class is made which is what I actually touch when talking to the controller in my handlebars template.

Is it possible to directly access that instantiated object from within the javascript console (or from within my program)? More generally, where do the objects that Ember automatically makes live?

like image 873
BostonJohn Avatar asked Jan 24 '13 16:01

BostonJohn


1 Answers

A class CheeseController is created and that when I activate the Cheese route an instance of that class is made which is what I actually touch when talking to the controller in my handlebars template.

Yes that is exactly what happens. Ember creates a singleton instance of App.CheeseController and provides it as the context when rendering your handlebars template.

Is it possible to directly access that instantiated object from within the javascript console

Yes. The best way to do this from the javascript console is by using the handlebars {{debugger}} helper from your template. This will open the JS debug console in the context of your template.

<script type="text/x-handlebars" data-template-name="cheese">
  {{debugger}}
</script>

With debugger open, you can access the instantiated controller singleton as this, so this.toString() should return something like <App.CheeseController:ember225>.

(or from within my program)?

Depends on which part of your program

  • From a route: Use this.controllerFor('cheese')
  • From a model: No. Please don't access controllers from models.
  • From another controller: If you declare a dependency in the other controller, needs: ['cheese'] then the singleton App.CheeseController will be accessible from the other controller via the property controllers.cheese. See Automatically synthesize the controller 'needs' dependencies
  • From a template: Use the needs array to declare a dependency from the templates controller, then from within your template the cheese controller is at: {{controllers.cheese}}

It is also possible to access the cheeseController instance via the ember container, but please don't. The container is not meant to be a public API. Recent updates to Ember have made accessing it somewhat awkward. This is because using global constants to access instances breaks encapsulation, and while that is fine for the console it should be avoided in your application code. For more detail, see App.container was not meant to be a public API

More generally, where do the objects that Ember automatically makes live? Internally ember caches controller singletons in the container. Of course it is not a part of the public API, but if you are curious about how things work internally check out container_test.js and What is the purpose of the Ember.Container

like image 90
Mike Grassotti Avatar answered Nov 07 '22 20:11

Mike Grassotti