Multiple Layouts in Ember.js?

随声附和 提交于 2019-12-21 17:40:39

问题


Coming from a Rails background, you can have multiple Layouts - for say, anonymous user pages and then authenticated pages.

Is this possible with Ember?

I've tried declaring a new templateName in my UsersRouter, with no avail.

I've also checked this guide: http://emberjs.com/guides/views/adding-layouts-to-views/

But it doesn't seem to be working :/


回答1:


You can use {{render}} inside an if helper to show different layouts.

For instance if you have an ApplicationController that has login and logout action handlers, and a corresponding `loggedIn' property.

App.ApplicationController = Ember.Controller.extend({
  loggedIn: false,

  login: function() {
    this.set('loggedIn', true);
  },

  logout: function() {
    this.set('loggedIn', false);
  }
});

The you can bind to the loggedIn property inside the application template like so.

<script type='text/x-handlebars' data-template-name='application'>
<button {{action login }}>Login</button>
<button {{action logout }}>Logout</button>

{{#if loggedIn}}
  {{render 'user'}}
{{else}}
  {{render 'guest'}}
{{/if}}

</script>

Where user and guest are corresponding templates.

<script type='text/x-handlebars' data-template-name='user'>
<h1>User layout</h1>
<div class='box user'>
{{outlet}}
</div>
</script>

<script type='text/x-handlebars' data-template-name='guest'>
<h1>Guest layout</h1>
<div class='box guest'>
{{outlet}}
</div>
</script>

Here's a working jsbin.

Edit: To not use the application route based on some static criteria or loaded via model hooks, you can override the renderTemplate method of the ApplicationRoute.

App.ApplicationRoute = Ember.Route.extend({
  renderTemplate: function() {
    var loggedIn = false;
    if (loggedIn) {
      this.render('user');
    } else {
      this.render('guest');
    }
  }
});


来源:https://stackoverflow.com/questions/17285660/multiple-layouts-in-ember-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!