什么ember.js组件负责将模板插入到DOM中? [英] What ember.js component is responsible for inserting templates into the DOM?

查看:77
本文介绍了什么ember.js组件负责将模板插入到DOM中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个ember.js / rails应用程序。所有手柄模板都存储在.js文件中。我想了解当路由器更改状态时如何将其插入到DOM中。 ember的哪个部分是这样的?如何告诉ember放置模板?



现在我只能将我的模板附加到< body> 我有一个 jsFiddle这里



我知道在Ember.Application上设置 rootElement ,但是我想让应用程序控制页面的其他元素。 / p>

Handlebars / HTML

 < script type =text / x-handlebarsdata-template-name =application> 
< h2>我是内容< / h2>
< p>这应该在标题& amp; amp; amp;页脚< / p为H.
< p>< strong>时间< / strong> {{时间}}< / p为H.
< / script>

< header>
< h1>应用< / h1>
< / header>
< article>
< div id =content>< / div>
< / article>
< footer>
< a href =http://blog.deanbrundage.comtarget =_ blank>由Dean< / a>
< / footer>

JavaScript

  window.App = Ember.Application.create(); 

App.Router = Em.Router.extend({
initialState:'root.home',
root:Em.Route.extend({
home: Em.Route.extend({
view:App.ApplicationView
})
})
});

App.ApplicationController = Em.Controller.extend({
time:Date()
});
App.ApplicationView = Em.View.extend({
templateName:'application'
});

App.initialize();


解决方案

Ember.js中的当前实现追加 ApplicationView 到应用程序的 rootElement ,请参阅 here



一个可能的解决方案是覆盖 ApplicationView appendTo ,请参阅 http://jsfiddle.net/pangratz666/m8L6Z/



JavaScript

  App.ApplicationView = Em.View.extend({
templateName:'application',
appendTo:function(){
this._super('#content');
}
});


I am building an ember.js/rails application. All the handlebar templates are stored in .js files. I'd like to understand how they get inserted into the DOM when the router changes state. Which part of ember does this? How do I tell ember to place templates?

Right now I can only get my templates appended to the <body> I have a jsFiddle here.

I am aware of setting rootElement on Ember.Application, however I want the app to control other elements of the page.

Handlebars / HTML:

<script type="text/x-handlebars" data-template-name="application">
    <h2>I'm the content</h2>
    <p>This should be inbetween the header &amp; footer</p>
    <p><strong>time</strong> {{time}}</p>
</script>

<header>
    <h1>Application</h1>
</header>
<article>
    <div id="content"></div>
</article>
<footer>
    <a href="http://blog.deanbrundage.com" target="_blank">by Dean</a>
</footer>

​JavaScript:

window.App = Ember.Application.create();

App.Router = Em.Router.extend({
    initialState: 'root.home',
    root: Em.Route.extend({
        home: Em.Route.extend({
            view: App.ApplicationView
        })
    })
});

App.ApplicationController = Em.Controller.extend({
    time: Date()
});
App.ApplicationView = Em.View.extend({
    templateName: 'application'
});

App.initialize();

解决方案

The current implementation in Ember.js appends the ApplicationView to the rootElement of the Application, see here.

One possible solution would be to overwrite the appendTo of your ApplicationView, see http://jsfiddle.net/pangratz666/m8L6Z/:

JavaScript:

App.ApplicationView = Em.View.extend({
    templateName: 'application',
    appendTo: function(){
        this._super('#content');
    }
});

这篇关于什么ember.js组件负责将模板插入到DOM中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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