访问控制器的实例或以ember形式的视图 [英] Accessing an instance of a controller or a view in ember

查看:103
本文介绍了访问控制器的实例或以ember形式的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,当我运行

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?

推荐答案


创建一个类CheeseController,当我激活Cheese路由时,该类的一个实例是在我的手柄模板中与控制器通话时实际触摸的。

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.

是的,这正是发生了什么。 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.


是否可以直接访问$控制台内实例化的对象

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

是的。从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>.


(或从我的程序中)?

(or from within my program)?

取决于您程序的哪一部分

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 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}}

还可以通过ember访问cheeseController实例容器,但请不要。容器不是公开的API。 Ember最近的更新使得访问有点尴尬。这是因为使用全局常量来访问实例会中断封装,而对于控制台而言,应该避免在应用程序代码中。有关详细信息,请参阅 App.container不是公开API

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的一部分,但是如果你对内部的工作感到好奇,请查看 container_test.js
Ember.Container的用途是什么

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

这篇关于访问控制器的实例或以ember形式的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆