【问题标题】:ember.js + ember-simple-auth how can I load application models after authentication?ember.js + ember-simple-auth 如何在身份验证后加载应用程序模型?
【发布时间】:2014-06-12 11:11:59
【问题描述】:

我正在添加 ember-simple-auth 来处理我正在构建的应用程序的身份验证。目前在 ApplicationRoute 中,我正在使用模型来加载侧边栏内容。

某些数据取决于用户 URL 属性,该属性与身份验证令牌一起返回。

我正在重构我的代码以处理经过身份验证的用户的加载数据,但我不确定在哪里放置模型调用以加载侧边栏数据。

我认为在 isAuthenticated 属性上添加观察者以触发模型加载或采用我当前的路由并将它们包装在负责加载模型的资源中是否有意义?

申请途径

App.ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin,
{
    model: function()
    {
        return Ember.RSVP.hash(
        {
            collections: Ember.$.getJSON(this.session.get('user.url') + '/collection'),

            libraries: Ember.$.getJSON(ENV.api + '/library')
        });
    },


    setupController: function(controller, model)
    {
        controller.set('libraries', model.libraries);

        controller.set('collections', model.collections);
    }
});

路线图

App.Router.map(function()
{
    this.route('login');

    // Authenticated Routes

    this.route('my-account');

    this.route('collection', { path: '/collection/:id' });

    this.route('item.new', { path: '/item/new' });

    this.route('item.edit', { path: '/item/:id' });

    this.route('library', { path: '/:slug' });
});

【问题讨论】:

    标签: authentication ember.js ember-simple-auth


    【解决方案1】:

    您可以在路由中使用this.get('session.user_email') 来获取经过身份验证的用户的电子邮件,然后使用它从服务器获取用户数据。

    【讨论】:

    • 我最终做了类似的事情,只是将用户 ID 输入到响应包中。
    【解决方案2】:

    ApplicationRouteMixin 定义了当会话状态从未认证变为已认证时调用的 sessionAuthenticationSucceeded action。也可以收听本次会议的sessionAuthenticationSucceeded event

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多