Ember.js:HtmlBars和Handlebars.compile命令

时间:2015-01-29 11:14:04

标签: ember.js ember-cli

运行我的应用时出现以下错误:

Uncaught Error: Cannot call `compile` without the template compiler loaded. Please load `ember-template-compiler.js` prior to calling `compile`.

这与这段代码有关:

var CarouselView = Ember.View.extend({
    template: Ember.Handlebars.compile('{{view view.itemsView}}'),
    elementId: 'carousel',
    contentBinding: 'content',
    ...

在ember.js github上已经存在与此问题相关的问题:https://github.com/emberjs/ember.js/issues/10265

但是我在我的package.json中添加了ember-template-compiler并再次遇到了同样的错误。

我做了: npm install --save-dev ember-template-compiler

这是我的package.json devDependencies:

 "ember-cli": "0.1.10",
 "ember-cli-app-version": "0.3.0",
 "ember-cli-content-security-policy": "0.3.0",
 "ember-cli-dependency-checker": "0.0.7",
 "ember-cli-htmlbars": "^0.6.0",
 "ember-cli-ic-ajax": "0.1.1",
 "ember-cli-inject-live-reload": "^1.3.0",
 "ember-cli-qunit": "0.3.0",
 "ember-cli-simple-auth": "^0.7.2",
 "ember-cli-simple-auth-cookie-store": "^0.7.2",
 "ember-cli-simple-auth-oauth2": "^0.7.2",
 "ember-cli-uglify": "1.0.1",
 "ember-data": "1.0.0-beta.12",
 "ember-export-application-global": "^1.0.0",
 "ember-template-compiler": "^1.8.0",
 "express": "^4.8.5",
 "glob": "^4.0.5"

参考:ember-template-compiler github page

任何人都有使用HtmlBars和编译命令的经验吗?

3 个答案:

答案 0 :(得分:16)

由于Ember.js 1.10模板编译器是Ember的一部分,因此在客户端编译模板所需要做的就是在Brocfile中添加以下行:

app.import('bower_components/ember/ember-template-compiler.js');

答案 1 :(得分:11)

您正尝试在客户端上编译HTMLBars模板,但在ember-template-compiler中添加package.json只能启用服务器端HTMLBars模板的预编译。

要启用客户端编译,您应该将ember-template-compiler添加到bower.json,例如(使用适当的版本)

"ember-template-compiler": "http://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"

然后将其包含在Brocfile.js

app.import('bower_components/ember-template-compiler/index.js');

答案 2 :(得分:1)

对于我的观点,我刚为他们创建了模板文件。以您的案例为例,我将创建app / templates / views / carousel.hbs:

[1,2,3,6,22,98,45,23,22,12];

然后CarouselView变为:

{{view view.itemsView}}

这样您就不必为客户端提供模板编译器。应该可以为客户端提供更好的性能和更小的负载。