EmberJS - 注册预编译的车把模板

时间:2013-03-01 23:04:18

标签: javascript ember.js handlebars.js

对于我的EmberJS应用程序,我预先编译了所有的把手模板,因此它们被加载为直接的Javascript文件。

问题是这些预编译的模板没有像我认为的那样进入Ember容器 - 当我为我的视图指定模板时,我收到以下错误。

Uncaught Error: assertion failed: You specified the templateName "application" for <MyApp.ApplicationView:ember164>, but it did not exist. 

这是我的观看代码。

window.MyApp.ApplicationView = Ember.View.extend({
   templateName: 'application'
});

我完成了执行,发现Ember容器中不存在视图。使用容器注册预编译模板时,我需要做些什么特别的事情吗?如果是这样,怎么样?

编辑:我一直在使用把手npm包编译模板。

3 个答案:

答案 0 :(得分:3)

模板在Ember.TEMPLATES上查找(这只是一个以模板名称为键的哈希)

因此,当您的示例ApplicationView被执行时,它将在Ember.TEMPLATES['application']

中查找模板

答案 1 :(得分:1)

虽然npm把手编译器确实正确地编译它们,但你仍然需要用Ember注册它们才能正确加载它们。您可以执行以下操作之一:

  • 使用Ember.TEMPLATES ['sometemplate'] = COMPILED TEMPLATE手动加载它们。这有效但却变得很痛苦。
  • 使用像npm ember-precompile这样的特殊编译器,它将以编译后的模板在Ember模板容器中自动注册的方式编译它们。

答案 2 :(得分:0)

如果您更喜欢基于Ruby / Guard的解决方案,请查看我的要点:https://gist.github.com/perlun/5286391

在Guardfile中使用它:

guard 'ember_handlebars',
    :input => 'app/handlebars_templates',
    :output => 'app/handlebars_compiled',
    :remove_prefix => 'app/handlebars_templates/' do
  watch(%r{app/handlebars_templates/(.+\.handlebars)})
end

```