Ember Simple Auth - 将当前用户注入每条路线 [英] Ember Simple Auth - injecting current user into every route

查看:18
本文介绍了Ember Simple Auth - 将当前用户注入每条路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ember Simple Auth 构建网站.

I am building a site using Ember Simple Auth.

我按照这些说明尝试添加当前用户对象到会话并且它工作,使用这个稍微修改的代码:

I followed these instructions to try and add the current user object to the session and it worked, using this slightly adapted code:

import Ember from 'ember';
import Session from 'simple-auth/session';

export default {
  name: "current-user",
  before: "simple-auth",
  initialize: function(container) {
    Session.reopen({
      setCurrentUser: function() {
        var accessToken = this.get('secure.token');
        var _this = this;
        if (!Ember.isEmpty(accessToken)) {
          return container.lookup('store:main').find('user', 'me').then(function(user) {
            _this.set('content.currentUser', user);
          });
        }
      }.observes('secure.token'),
      setAccount: function() {
        var _this = this;
        return container.lookup('store:main').find('account', this.get('content.currentUser.account.content.id')).then(function(account) {
          _this.set('content.account', account);
        });
      }.observes('content.currentUser'),
    });
  }
};

但是,使用最新版本的 Ember,我得到了以下信息:

However, using the latest version of Ember I'm getting the following:

弃用:lookup 在注册表上被调用.initializer API 不再接收容器,您应该使用 instanceInitializer 从容器中查找对象.见 http://emberjs.com/guides/deprecations#toc_deprecate-access-to-instances-in-initializers 了解更多详情.

DEPRECATION: lookup was called on a Registry. The initializer API no longer receives a container, and you should use an instanceInitializer to look up objects from the container. See http://emberjs.com/guides/deprecations#toc_deprecate-access-to-instances-in-initializers for more details.

我知道我需要将上述内容拆分为/app/initializers 和/app/instance-initializers(根据注释 此处) 但我不太确定如何去做.

I know that I need to split the above into /app/initializers and /app/instance-initializers (as per the notes here) but I'm not quite sure how to go about it.

当然,如果有一种更简单/更简洁的方法可以让用户和帐户对象可用于每个路由/模板,我很乐意听到它们:)

Of course, if there is an easier/cleaner way to make the user and account objects available to every route/template I'd love to hear them :)

谢谢

推荐答案

这对我有用:

  • ember-cli:0.2.7(ember:1.12.0,ember-data:1.0.0-beta.18)
  • ember-cli-simple-auth: 0.8.0-beta.3

注意:

  • 余烬数据:1.13.商店已在初始化程序中注册,应按原样工作
  • 余烬数据:1.0.0-beta.19.Store 在实例初始化器中注册,需要一些调整

1) 自定义会话

//config/environment.js
ENV['simple-auth'] = {
  session: 'session:custom',
  ...
}

//app/sessions/custom.js
import Session from 'simple-auth/session';

export default Session.extend({

  // here _store is ember-data store injected by initializer
  // why "_store"? because "store" is already used by simple-auth as localStorage
  // why initializer? I tried 
  // _store: Ember.inject.service('store') and got error

  currentUser: function() {
    var userId = this.get('secure.userId');
    if (userId && this.get('isAuthenticated')) {
      return this._store.find('user', userId);
    }
  }.property('secure.userId', 'isAuthenticated')

});

2) 通过初始化程序将存储注入会话(否则 find() 将不起作用)

2) Inject store to session by initializer (otherwise find() wouldn't work)

//app/initializers/session-store
export function initialize(container, application) {
  application.inject('session:custom', '_store', 'store:main')

  // "store:main" is highly dynamic depepeding on ember-data version
  // in 1.0.0-beta.19 (June 5, 2015) => "store:application"
  // in 1.13 (June 16, 2015) => "service:store"
}

export default {
  name: 'session-store',
  after: 'ember-data',
  initialize: initialize
}

3) 在模板中

{{#if session.isAuthenticated}}
  {{session.currentUser.name}}
{{/if}}

注意:这并不能免除您由 ember-simple-auth 本身产生的弃用.

Note: this does not relieve you from deprecations generated by ember-simple-auth itself.

这篇关于Ember Simple Auth - 将当前用户注入每条路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆