问题描述
我的理解是,当我运行
App.CheeseController = Ember.Controller.extend({ type:"brie"});
创建一个类 CheeseController
我激活了奶酪路线,该类的一个实例是我在我的车把模板中与控制器通话时实际触摸的实例。
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.
可以从javascript控制台(或从我的程序)直接访问该实例化对象吗?更一般地说,Ember自动生成的对象在哪里?
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?
推荐答案
是的,这正是发生了什么。 Ember创建一个App.CheeseController的单例实例,并在渲染你的handlebars模板时提供它作为上下文。
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.
是的。从javascript控制台执行此操作的最佳方法是使用模板中的handlebars {{debugger}}
帮助器。这将在您的模板的上下文中打开JS调试控制台。
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>
调试器打开后,您可以访问实例化的控制器单例作为 code>,所以
this.toString()
应该返回类似< App.CheeseController:ember225>
With debugger open, you can access the instantiated controller singleton as
this
, so this.toString()
should return something like <App.CheeseController:ember225>
.
取决于您程序的哪一部分
Depends on which part of your program
- 从路由:使用
this.controllerFor('cheese ')
- 从型号:否请不要从型号访问控制器。
- 从另一个控制器:如果您在其他控制器中声明依赖关系,则
需要:['cheese']
然后单例App.CheeseController
将可以通过其他控制器通过属性controllers.cheese
访问。请参阅 - 从模板中:使用
需要
数组从模板控制器声明依赖关系,然后从模板中获取奶酪控制器:{{controllers.cheese}}
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' dependenciesFrom 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}}
还可以通过ember访问cheeseController实例容器,但请不要。容器不是公开的API。 Ember最近的更新使得访问有点尴尬。这是因为使用全局常量来访问实例会中断封装,而对于控制台而言,应该避免在应用程序代码中。有关详细信息,请参阅
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
更一般地说,Ember自动生成的对象在哪里?
内部的ember缓存容器中的控制器单例。当然,这不是公共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 andWhat is the purpose of the Ember.Container
这篇关于访问控制器的实例或以ember形式的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!