谁能解释一下最新的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>"));
发布于 2012-12-30 15:35:49
容器的目标是提供一种比我们一直使用的ad-hoc方法更通用的机制来描述模块依赖关系。
例如,假设您想要查找post
路由的控制器。默认的Ember规则是我们将其查找为App.PostController
。在容器之前,我们只需在需要查找的地方硬编码这些规则(使用classify
和朋友)。
容器为我们提供了一种在单一位置定义这些规则的方法。额外的好处是,对于需要不同约定的应用程序,可以覆盖这些规则。
因此,我们现在使用container.lookup('controller:' + name)
,而不是内部的Ember.get(namespace, Ember.String.classify(name) + 'Controller')
。
发布于 2013-01-07 18:12:45
https://stackoverflow.com/questions/14085749
复制相似问题