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

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

问题描述

我正在使用Ember Simple Auth构建一个网站。



我遵循这些说明尝试将当前用户对象添加到会话中,并使用这种稍微调整的代码:



从ember导入Ember
import 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')然后(function(user){
_this.set
}
} .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,我得到以下内容:


DEPRECATION: lookup 在注册表中被调用。 initializer API不再收到容器,您应该使用 instanceInitializer 从容器中查找对象。请参阅 http://emberjs.com/guides/deprecations#toc_deprecate-access-to-instances -in初始化器更多细节。


我知道我需要将以上内容分成/ app / initializers和/ app / instance-initializationizers(根据注释 here )但是我不太清楚如何去做。



当然,如果有更简单/更清洁的方法来使每个路由都可以使用用户和帐户对象/ template我很想听到他们:)



谢谢

解决方案

p>这对我有用:




  • 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



注意:




  • ember数据:1.13。商店在初始化器中注册,应该正常工作。

  • ember-data:1.0.0-beta.19。商店在实例初始化器中注册,需要进行一些调整



1)自定义会话

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

//app/sessions/custom.js
import'simple-auth / session'的会话;

导出默认Session.extend({

//这里_store是由初始化程序注入的ember-data store
//为什么_store?因为store已经使用simple-auth作为localStorage
//为什么是初始化程序?我试过
// _store:Ember.inject.service('store')并且收到错误

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

});

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

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

//store:main是高度动态的依赖于ember数据版本
// in 1.0。 0-beta.19(2015年6月5日)=> store:application
// in 1.13(2015年6月16日)=> service:store
}

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

3)在模板中

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

注意:这不能免除您由 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'),
    });
  }
};

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

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.

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 :)

Thanks

解决方案

This works for me on:

  • 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

Note:

  • ember-data: 1.13. Store is registered in an initializer, should work as is
  • ember-data: 1.0.0-beta.19. Store is registered in an instance-initializer, some adjustments needed

1) Customize session

//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) 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) In template

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

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

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

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