我刚刚开始熟悉 ember.js,并想用它创建一个单页应用程序。 由于应用程序的性质,我有时会有多个“主要”布局。
用户登录应用程序后,我需要根据 ajax 调用显示用户名。我还不明白如何在 ember.js 中最好地实现这一点,因为这(至少对我来说)位于任何 View 层次结构之外。
我尝试使用渲染方法的“into”选项。
App.TestRoute = Ember.Route.extend({
renderTemplate: function(controller, model) {
this.render('layout');
this.render('test',{
into: 'layout'
});
},
});
我认为这也会调用布局路线并加载其模型。不幸的是它没有这样做。
我用 {{yield}} 指令定义了一个布局
<div>
<div class="container" id="header">
{{loginUser.userName}}
</div>
<div class="container" id="main-content">
{{yield}}
</div>
<div class="container" id="footer">
Just some static footer
</div>
</div>
我的观点是这样的
App.TestView = Ember.View.extend({
layoutName: 'layout'
});
渲染工作正常,但我无法看到哪里可以最好地注入(inject)用户数据。 理想情况下,这对于使用此布局的所有 View 应该是透明的。所以我的想法是使用继承,但还没有找到可行的解决方案。
我完全偏离了轨道还是这有道理?
更新
我把例子放在这里:http://jsfiddle.net/Efz8N/4/
最佳答案
布局由 Controller 支持, Controller 使用布局支持 View 。
loginUserFullName:function(){
return this.get('controllers.application.loginUser') || 'not in';
}.property('controllers.application.loginUser')
关于javascript - Ember.js 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20598273/