简体   繁体   English

Ember.js布局

[英]Ember.js Layout

I am just getting warm with ember.js and would like to create a single page application with it. 我刚刚对ember.js感到满意,并希望使用它创建一个单页应用程序。 Due to the nature of the application, I will have multiple "major" layouts at some point. 由于应用程序的性质,在某些时候我将有多个“主要”布局。

After the user logged into the application, I need to display the user name based on an ajax call. 用户登录到应用程序后,我需要基于ajax调用显示用户名。 I did not yet understand how I best implement this in ember.js, because this (at least to me) is outside of any view hierarchy. 我还不了解如何在ember.js中最好地实现这一点,因为(至少对我而言)这不在任何视图层次结构之内。

I played around with using the "into" option of the render method. 我使用了render方法的“ into”选项。

App.TestRoute = Ember.Route.extend({
  renderTemplate: function(controller, model) {
    this.render('layout');

    this.render('test',{
       into: 'layout'
    });
  },
});

I thought this would also call the layout route and load its model. 我认为这也会调用布局路线并加载其模型。 Unfortunately it did not do that. 不幸的是它没有那样做。

I went over to define a layout with a {{yield}} directive 我用{{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>

and my view like 我的看法像

App.TestView = Ember.View.extend({
  layoutName: 'layout'
});

The rendering works fine, but I fail to see where I can best inject the user data. 渲染工作正常,但是我看不到可以最好地注入用户数据的位置。 Ideally this should be transparent for all views that make use of this layout. 理想情况下,这对于使用此布局的所有视图应该是透明的。 So my thought was to work with inheritance, but did not come to a working solution yet. 所以我的想法是使用继承,但是还没有找到可行的解决方案。

Am I completely off track here or does this somehow make sense? 我在这里完全偏离轨道吗,或者这在某种程度上有意义?

UPDATE UPDATE

I put the example in here: http://jsfiddle.net/Efz8N/4/ 我将示例放在这里: http : //jsfiddle.net/Efz8N/4/

Layout's are backed by the controller which is backing the view using the layout. 布局由控制器支持,该控制器使用布局支持视图。

http://jsfiddle.net/WHvCZ/ http://jsfiddle.net/WHvCZ/

loginUserFullName:function(){
    return this.get('controllers.application.loginUser') || 'not in';
}.property('controllers.application.loginUser')

Here is how I did it now. 这就是我现在的做法。 I created a mixin that I can use in any Controller that makes use of the layout. 我创建了一个混合器,可以在任何使用布局的控制器中使用。 This way I at least do not have to implement this multiple times: 这样,我至少不必多次执行此操作:

App.LayoutHandler = Ember.Mixin.create({
  loginUserFullName: function() {
    var name = App.get("loginUser")?App.get("loginUser"):"";

    return name;
  }.property("App.loginUser"),
});

App.IndexController = Ember.Controller.extend(App.LayoutHandler, {

});

Here an example: http://jsfiddle.net/Efz8N/6/ 下面是一个示例: http : //jsfiddle.net/Efz8N/6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM