Ember.Container的目的是什么?

dag*_*da1 18 ember.js

任何人都可以解释最新的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>"));
Run Code Online (Sandbox Code Playgroud)

Yeh*_*atz 34

容器的目标是提供一种更通用的机制来描述模块依赖性,而不是我们一直使用的临时方法.

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

容器为我们提供了在单个位置定义这些规则的方法.作为奖励,可以为需要不同约定的应用程序覆盖规则.

因此Ember.get(namespace, Ember.String.classify(name) + 'Controller'),我们现在做的不是内部container.lookup('controller:' + name).

  • 但据我所知,这仅用于内部API,而不是公共API,对吧? (3认同)