【问题标题】:Rails: Ember.js Handlebar templates not being recognizedRails:Ember.js Handlebar 模板无法识别
【发布时间】:2014-02-27 16:12:00
【问题描述】:

我有一个相当基本的问题。

我正在学习将 Ember.js 与 Rails 后端一起使用。当在 HTML 文件中内联提到模板时,我可以让事情正常工作。

<script type="text/x-handlebars" id="index">
<div class="container-fluid">
    <div class="row-fluid">
        <div class="span3">
            <table class='table'>
                <thead>
                <tr><th>Recent Posts</th></tr>
                </thead>
                {{#each item in model}}
                    <tr><td>
                        {{item.name}}
                    </td></tr>
                {{/each}}
            </table>
        </div>
        <div class="span9">
            {{outlet}}
        </div>
    </div>
</div>
</script>

但是当我将这个 sn-p 移动到模板目录下的 .hbs 文件中,并在我的 application.js 文件中包含以下行时,

#= require_tree ./templates

他们因这个错误而失败。

Assertion failed: You specified the templateName cms2/index for <CMS.IndexView:ember229>, but it did not exist. 

我尝试使用两个不同的 Rubygems 进行预编译,

gem 'handlebars_assets'

gem 'ember-rails'  

根据我的阅读,这两个 gem 应该预编译所有 .hbs 文件,包括模板目录树应该可以解决这个问题。

我错过了什么?为什么这对我不起作用?

谢谢。

【问题讨论】:

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


    【解决方案1】:

    您是否在 Handlebars Assets 中启用了 Ember 支持?

    HandlebarsAssets::Config.ember = true
    

    这会将模板添加到 Ember.TEMPLATES 命名空间,以便解析它们。

    【讨论】:

    • buuda - 感谢您的提示。我没有添加它,但是将它添加到 environment.rb 并没有修复错误。然后我注意到我在 application.js 中专门添加了 handlebars-1.0.0。我将它替换为 #= 需要车把并修复了错误。但是现在,模板没有被加载,甚至应用程序模板也没有。 Ember.TEMPLATES 表示以下内容:Object {../cms2/templates/application: function, ../cms2/templates/index: function} ../cms2/templates/application: function (context, options) { ../cms2/templates/index: function (context, options) {
    • 我得到了这个工作,但感觉就像一个黑客。我必须创建两个视图并像这样设置模板:window.CMS.IndexView = Ember.View.extend template: Ember.TEMPLATES['../cms2/index'] 这似乎可行。如果我使用 templateName,它就不起作用。我一定遗漏了一些明显的东西。无论如何感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多