Javascript 余烬延迟控制器,直到身份验证完成

Javascript 余烬延迟控制器,直到身份验证完成,javascript,ember.js,Javascript,Ember.js,我有一种情况,我需要获取会话的一些属性,但是我还没有找到一个成功的解决方案来延迟成功登录后路由的加载 所以这里的交易-当用户登录时,他们会被发送到电话号码路由。尚未设置应用程序控制器上的this.get('session.currentUser') 当我走另一条路线,然后回来时,它被正确设置。如果我在登录后使用电话号码路由,然后刷新页面,则由于初始值设定项中的延迟准备和高级准备,电话号码会正确加载。我无法在登录前推迟准备,因为应用程序已加载并准备就绪 唯一缺少的部分是,在用户登录后,它应该加载路

我有一种情况,我需要获取会话的一些属性,但是我还没有找到一个成功的解决方案来延迟成功登录后路由的加载

所以这里的交易-当用户登录时,他们会被发送到电话号码路由。尚未设置应用程序控制器上的this.get('session.currentUser')

当我走另一条路线,然后回来时,它被正确设置。如果我在登录后使用电话号码路由,然后刷新页面,则由于初始值设定项中的延迟准备和高级准备,电话号码会正确加载。我无法在登录前推迟准备,因为应用程序已加载并准备就绪

唯一缺少的部分是,在用户登录后,它应该加载
路由/电话号码.js
中的号码,这是粘贴在下面的最后一块代码。但是,
myStoreId
未加载,因为尚未设置
会话.currentUser

我已经尝试了很多事情来让它工作,我正在寻找一些关于这最后一部分的想法。它离工作很近,只是少了一小块

// initializers/current-user.js
import Ember from 'ember';
import Session from 'simple-auth/session';

export default {
  name: 'current-user',
  before: 'simple-auth',

  initialize: function(container, application) {
    Session.reopen({
      setCurrentUser: function() {
        let appController = container.lookup("controller:application");

        // don't know how to check if the app is already ready
        try{
          application.deferReadiness();
          console.log('deferred');
        }catch(e){}

        if(this.get('isAuthenticated')) {
          console.log('running the isAuthenticated obs');

          let store = container.lookup('store:main');
          let _this = this;

          return store.find('user', 'me').then((user) => {
            // set the current user to be used on the session object
            this.set('currentUser', user);
          }).then(function(){
            // set the store for the current user
            store.find('store', {user: _this.get('currentUser.id')}).then(function(data) {
              _this.set('myStore', data.get('firstObject'));
              application.advanceReadiness();
            });
          })
        }
      }.observes('isAuthenticated')
    });
  }
};

// controllers/application.js
export default Ember.Controller.extend({
  myStore: Ember.computed(function(){
    // return the store object that is associated with the current user
    if(this.get('session.isAuthenticated')){
      if(this.get('session.myStore')){
        return this.get('session.myStore');
      }else{
        console.log(this.get('session.currentUser'));
        // this is where the problem is. The session.currentUser is not populated yet.

        this.get('store').find('store', {user: this.get('session.currentUser.id')}).then(function(data) {
          this.get('session').set('myStore', data.get('firstObject'));
          return this.get('session.myStore');
        });
      }
    }
  }),
});


// routes/phone-numbers.js
export default Ember.Route.extend({
  setupController: function(controller, model){
    this._super(controller, model);
    let myStoreId = this.controllerFor('application').get('myStore.id');

    if(!myStoreId){
      console.log(this.get('session.currentUser'));
      // there is no currentUser set on the session after login
    }else{
      this.store.find('store-phone-number', {'store': myStoreId}).then(function(numbers){
        controller.set('numbers', numbers);
      });
    }
  },
});
尝试使用承诺:

// controllers/application.js
export default Ember.Controller.extend({
  myStore: Ember.computed('session.currentUser', function(){
    // return the store object that is associated with the current user
    return new Ember.RSVP.Promise(resolve => {
      if(this.get('session.isAuthenticated')){
        if(this.get('session.myStore')){
          resolve(this.get('session.myStore'));
        } else {
          console.log(this.get('session.currentUser'));
          // this is where the problem is. The session.currentUser is not populated yet.

          this.get('store').find('store', {user: this.get('session.currentUser.id')}).then(function(data) {
            this.get('session').set('myStore', data.get('firstObject'));
            resolve(this.get('session.myStore'));
          });
        }
      }
    });
  })
});


// routes/phone-numbers.js
export default Ember.Route.extend({
  setupController: function(controller, model){
    this._super(controller, model);
    let myStoreId = this.controllerFor('application').get('myStore').then(myStore => {
      let myStoreId = myStore.get('id');
      if(!myStoreId){
        console.log(this.get('session.currentUser'));
        // there is no currentUser set on the session after login
      } else {
        this.store.find('store-phone-number', {'store': myStoreId}).then(function(numbers){
          controller.set('numbers', numbers);
        });
      }
    });
  }
});

丹尼尔提出的玩弄诺言的建议使我找到了解决办法。基本上,如果myStore尚未设置,我需要返回一个承诺,并且在登录后的第一个路径中也必须说明这一点

export default Ember.Controller.extend({
  myStore: Ember.computed(function(){
    // return the store object that is associated with the current user
    if(this.get('session.isAuthenticated')){
      if(this.get('session.myStore')){
        return this.get('session.myStore');
      }else{
        return new Promise(resolve =>{
          this.get('session').setCurrentUser().then(data => {
            this.get('store').find('store', {user: this.get('session.currentUser.id')}).then(data => {
              this.get('session').set('myStore', data.get('firstObject'));
              resolve(this.get('session.myStore'));
            });
          });
        })
      }
    }
  }),
});

export default Ember.Route.extend({
  setNumbers: function(controller, id){
    this.store.find('store-phone-number', {'store': id}).then(numbers => {
      controller.set('numbers', numbers);
    });
  },
  setupController: function(controller, model){
    this._super(controller, model);

    if(this.controllerFor('application').get('myStore.id')){
      let myStoreId = this.controllerFor('application').get('myStore.id')
      this.setNumbers(controller, myStoreId);
    }else{
      let myStore = this.controllerFor('application').get('myStore').then(data => {
        this.setNumbers(controller, data.id);
      });
    }
  },
});

谢谢,我来玩玩这个。在本例中,承诺中的
this.get('session.currentUser.id')
在登录时仍然不可用,并且在刷新/路由时,它没有定义并给出错误