【问题标题】:Ember.js RC1 - view not displayed when specifying 'template'Ember.js RC1 - 指定“模板”时不显示视图
【发布时间】:2013-03-28 07:00:37
【问题描述】:

按照 Ember 模板指南,我执行以下操作:

App.PostsView = Ember.View.extend({
    template: Ember.Handlebars.compile('I am the template')
});

但是没有渲染任何东西。当我使用 templateName 时,视图 呈现:

// html

<script type='text/x-handlebars' data-template-name='posts-template'>
    <h1>I am the template</h1>
</script>

// js

App.PostsView = Ember.View.extend({
    templateName: 'posts-template'
});

如何使用指南中所述的template 函数使模板正常工作?

编辑:

我确实遇到过这些:

emberjs template compile doesn't work in rc1

How do I specify using a view with a compiled handlebar in ember.js RC1

所以这个可能看起来像复制品。然而,这首先似乎使用了一些未在 Ember 指南中指定的额外工作,第二个是我正在做的,但它不起作用。

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    好的,这可行:

    Ember.TEMPLATES['posts-template'] = Ember.Handlebars.compile('I am the template');
    App.PostsView = Ember.View.extend({
        templateName: 'posts-template'
    });
    

    我们的情况是我们的预编译模板被添加到Handlebars.templates而不是Ember.TEMPLATES,同时我们使用这个返回编译模板,例如:

    App.PostsView = Ember.View.extend({
        template: Handlebars.templates['posts-template']
    });
    

    因此,似乎应该避免干扰template 函数,而是始终使用templateName,它将在Ember.TEMPLATES 中查找该名称。

    希望这可以帮助某人节省一些时间:)

    【讨论】:

      【解决方案2】:

      您还可以指定预期的templateName 以及实际的template 内容:

      App.PostsView = Ember.View.extend({
        templateName: 'posts',
        template: Ember.Handlebars.compile('I am the template')
      });
      

      JSBin example

      【讨论】:

      猜你喜欢
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      • 2020-07-05
      相关资源
      最近更新 更多