2012-12-29 47 views
18

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

其使用的一个例子,在设置和在该试验开始:

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>")); 
+0

我认为(和希望),这将被记录在案。我知道@tchak已经在这方面做过工作,也许他可以在这里解释一下。 –

回答

33

容器的目的是用于描述比AD-模块依赖关系提供了更通用的机构我们一直在使用这种方法。

例如,假设您想要找到post路线的控制器。默认的Ember规则是我们会将其查询为App.PostController。在容器之前,我们只需要在需要查找的地方硬编码这些规则(使用classify和朋友)。

容器为我们提供了一种在单个位置定义这些规则的方法。作为奖励,对于需要不同约定的应用程序,可以覆盖规则。我们现在做container.lookup('controller:' + name)

+3

但据我所知这只用于内部API,不是公共的,对吗? – ramigg

2
+2

Container的部分内容有些公开,可以(应该)用于注册/查找/注入依赖关系。您已链接的提交关于#controllerFor的讨论。即当已有公共机制时,您不应该访问容器(Ember将这些依赖注入抽象化)。 [“容器的公共API仍然不稳定。”](https://github.com/emberjs/ember.js/blob/master/packages/container/lib/main.js) – Michael