【问题标题】:Rails 4: Ember.js doesn't see my templates?Rails 4:Ember.js 看不到我的模板?
【发布时间】:2013-11-03 10:21:33
【问题描述】:

我正在尝试仅从 templates/about.handlebars 渲染模板。

我在 Rails 视图中设置了插座:

%script{ type: 'text/x-handlebars'}
  <h1> My app</h1>
  {{ outlet }}
  ^-- outlet

about 路由如下所示:

Admin.Router.map ->
  @route 'about', path: '/'

而about模板是纯html:

<h1>This is ABOUT!</h1>

什么似乎有效:

  • 我可以看到 Ember 调试消息,确认 ember 正在工作
  • 我安装了 ember chrome 扩展,它可以看到应用程序模板

但是,当我要求 Ember.TEMPLATES 时,我得到:

Object {application: function} 
application: function (context, options) { 
__proto__: Object

那里没有模板。

在编译好的js文件中挖掘,可以看到车把模板是物理存在的:

(function() {
  this.HandlebarsTemplates || (this.HandlebarsTemplates = {});
  this.HandlebarsTemplates["about"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
  this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};



  return "\n<h1>This is ABOUT!</h1>\n\n";
  });
  return this.HandlebarsTemplates["about"];
}).call(this);

但我没有看到它被明确添加到 Ember.TEMPLATES。

更新 1: 检查 ember-rails gem,我看到把手 will match only files that have .raw.(hjs|...), or mustache.(hjs|...) in their names?文档似乎没有谈到这一点。

即使我重命名模板,它仍然不显示,而且很奇怪,因为我没有在教程或 gem 文档中看到过这样的模板名称。

【问题讨论】:

  • Update 1 中的代码意味着,如果您有一个名为 someFile.raw.(handlebars|hjs|hbs) 的文件,ember-rails 将使用 Handlebars.compiler 而不是 Ember.Handlebars.compiler。 Ember.Handlebars.compiler 是 Handlebars.compiler 的扩展,允许在把手模板中使用计算属性、绑定等。可能这就是您将使用的。所以使用你的模板文件,中间没有raw

标签: ruby-on-rails ember.js handlebars.js


【解决方案1】:

Ember 在 Ember.TEMPLATES 中查找预编译模板,而不是在 HandlebarsTemplates 中。

【讨论】:

  • 车把是 1.0.12。如果我正确阅读了 ember-rails gem,则 ember-rails gem 负责将它们插入。
猜你喜欢
  • 2016-12-21
  • 2012-02-24
  • 1970-01-01
  • 2011-09-06
  • 1970-01-01
  • 2014-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多