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

问题描述

谁能解释一下最新的 Ember 中 Container 模块的用途是什么?

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.

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

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.

因此,我们现在在内部执行 container.lookup('controller:' + name).

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