我尝试在我的网络应用程序上将 EmberJS 从 pre1 更新到 pre2,但我注意到它将所有 Handlebars 模板作为最后一个主体元素,有时根本不这样做。
我已经create a repro使用 starter-kit 进行简单修改,将 div 放在模板之前和之后,以显示模板将添加到错误的位置。 使用 pre1 运行同一页面,一切正常。
index.html
...
<body>
<div>this is before the template</div>
<script type="text/x-handlebars" data-template-name="application">
<h1>Hello from Ember.js</h1>
</script>
<div>this is after the template</div>
...
最佳答案
text/x-handlebars 脚本标记定义了 Handlebar 模板,与您将其放置在页面中的位置(头部、正文中的任何位置)无关。
在上面的代码中,您定义了 ApplicationView
的模板。由于您使用路由器,ember 会自动创建 ApplicationView
并将其附加到您的 Ember 应用程序的根元素。默认情况下,rootElement
是正文:
App.ApplicationView.create().appendTo(rootElement) // default rootElement = 'body'
并且 appendTo
方法使用 jQuery appendTo :
this.$().appendTo(target)
因此,如果您想控制 applicationView
的插入位置,则需要在 App 实例中设置 rootElement
,如下所示:
window.App = Ember.Application.create({
rootElement: '#insert_my_app_here'
});
...
<body>
<div>this is before the template</div>
<script type="text/x-handlebars" data-template-name="application">
<h1>Hello from Ember.js</h1>
</script>
<div id="insert_my_app_here"></div>
<div>this is after the template</div>
</body>
关于ember.js - EmberJS pre2 将 Handlebars 模板放在错误的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13324556/