admin管理员组文章数量:1220454
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?
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?
Share Improve this question edited Jan 25, 2013 at 15:01 CraigTeegarden 8,2518 gold badges39 silver badges43 bronze badges asked Jan 24, 2013 at 16:38 BostonJohnBostonJohn 2,6612 gold badges28 silver badges48 bronze badges 1 |1 Answer
Reset to default 20A 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 singletonApp.CheeseController
will be accessible from the other controller via the propertycontrollers.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
本文标签: javascriptAccessing an instance of a controller or a view in emberStack Overflow
版权声明:本文标题:javascript - Accessing an instance of a controller or a view in ember - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739315921a2157806.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
App.__container__.lookup('controller:cheese')
. It also works with 'route:theRoute'. – lcoq Commented Jan 24, 2013 at 16:42