本文介绍了Ember.Container的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释最新的Ember中的容器模块的用途?



其中使用的一个例子,在设置和开始测试:$ {

  module(Ember.View  -  handlebars integration,{
setup:function(){
Ember.lookup = lookup = {Ember:Ember};
lookup.TemplateTests = TemplateTests = Ember.Namespace.create();

container = new Ember.Container();
container.optionsForType('template',{instantiate:false});
}

test(模板视图应该调用相关模板的函数,function(){
container.register('template','testTemplate',Ember.Handlebars.compile(< h1 id ='twas-called'>模板称为< / h1>));


解决方案

容器的目标是提供一个更通用的机制来描述模块依赖性比我们过去的ad-hoc方法使用。



例如,假设您想要找到 post 路由的控制器。默认的Ember规则是我们将它查找为 App.PostController 。在容器之前,我们只需要对这些规则进行硬编码,无论我们需要进行查找(使用 classify 和朋友)。



容器提供了一种在一个地方定义这些规则的方法。作为一个奖励,规则可以覆盖想要不同约定的应用程序。



所以而不是 Ember.get(命名空间,Ember.String .classify(name)+'Controller')在内部,我们现在做 container.lookup('controller:'+ name)。 >

Can anyone explain what the purpose of the Container module is in the latest Ember?

An example of its usage, in the setup and in the start of this test:

module("Ember.View - handlebars integration", {
  setup: function() {
    Ember.lookup = lookup = { Ember: Ember };
    lookup.TemplateTests = TemplateTests = Ember.Namespace.create();

    container = new Ember.Container();
    container.optionsForType('template', { instantiate: false });
  }

test("template view should call the function of the associated template", function() {
  container.register('template', 'testTemplate', Ember.Handlebars.compile("<h1 id='twas-called'>template was called</h1>"));
解决方案

The goal of the container is to provide a more general-purpose mechanism for describing module dependencies than the ad-hoc approach we had been using.

For example, imagine you want to find the controller for the post route. The default Ember rules are that we would look it up as App.PostController. Before the container, we would just hardcode those rules wherever we needed to do the lookup (using classify and friends).

The container provides a way for us to define those rules in a single place. As a bonus, the rules can be overridden for applications that want a different convention.

So instead of Ember.get(namespace, Ember.String.classify(name) + 'Controller') internally, we now do container.lookup('controller:' + name).

这篇关于Ember.Container的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 01:31