小编Vic*_*tor的帖子

如何使用新路由器重新呈现应用程序模板?

代码

我正在使用版本4fcdbf2560与新路由器.

在我的应用程序中,用户可以进行身份​​验证.根据身份验证状态,呈现的模板将不相同.

我通过重新定义以下函数renderTemplate来管理这个ApplicationRoute:

App.ApplicationRoute = Ember.Route.extend({
    renderTemplate: function() {
        this.render(App.authenticated() ? 'authenticated' : 'unauthenticated');
    }
});
Run Code Online (Sandbox Code Playgroud)

我的路由器非常简单:

App.Router.map(function(match) {
    match('/').to('index');

    match('/sign').to('sign', function(match) {
        match('/in').to('signIn');
    });

    match('/dashboard').to('dashboard');
});
Run Code Online (Sandbox Code Playgroud)

IndexRoute是就在这里根据认证状态,以将用户重定向:

App.IndexRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo(App.authenticated() ? 'dashboard' : 'signIn');
    }
});
Run Code Online (Sandbox Code Playgroud)

工作流程

  1. 用户导航到 /
  2. ApplicationRoute输入,为用户没有通过验证,unauthenticated模板被渲染
  3. IndexRoute输入,因为用户没有通过验证,重定向就是由signIn
  4. signIn模板被渲染成其父模板- >的unauthenticated模板
  5. 用户登录成功,route.transitionTo('dashboard')称为
  6. dashboard模板被渲染成其父模板- >的unauthenticated模板

问题

  • 我的实施有什么问题?
  • 为什么在渲染模板 …

ember.js

5
推荐指数
1
解决办法
1916
查看次数

标签 统计

ember.js ×1