Ember.js RC1 - 指定“模板"时不显示视图 [英] Ember.js RC1 - view not displayed when specifying 'template'

查看:17
本文介绍了Ember.js RC1 - 指定“模板"时不显示视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

App.PostsView = Ember.View.extend({模板: Ember.Handlebars.compile('我是模板')});

但是什么也没有渲染.当我使用 templateName 视图 is 呈现:

//html

//js

App.PostsView = Ember.View.extend({模板名称:'帖子模板'});

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

我确实遇到过这些:

emberjs 模板编译在 rc1 中不起作用

如何在 ember.js RC1 中指定使用带有编译把手的视图

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

解决方案

好的,这有效:

Ember.TEMPLATES['posts-template'] = Ember.Handlebars.compile('我是模板');App.PostsView = Ember.View.extend({模板名称:'帖子模板'});

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

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

所以看起来好像应该避免干扰 template 函数,而是 总是 使用 templateName 它将寻找那个Ember.TEMPLATES 中的名称.

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

Following the Ember guide on templates I do the following:

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

But nothing is rendered. When I use the templateName the view is rendered:

// 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'
});

How can I get the templates to work using the template function as stated in the guides?

edit:

I did come across these:

emberjs template compile doesn't work in rc1

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

So this might look like a duplicate. However, this first appears to use some extra work not specified in the Ember guides and the second is what I am doing but it is not working.

解决方案

OK, this works:

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

What was happening in our was that our pre-compiled templates were being added to Handlebars.templates and not Ember.TEMPLATES along with the fact that we were returning the complied template using this, e.g.:

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

So it seems as though one should refrain from interfering with the template function but rather always use the templateName which will be looking for that name in Ember.TEMPLATES.

Hopefully this helps someone save some time :)

这篇关于Ember.js RC1 - 指定“模板"时不显示视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆