2015-09-26 5 views
0

У меня возникла ситуация, когда мне нужно получить некоторые свойства на сеансе, но я не нашел успешного решения, чтобы отложить загрузку маршрута после успешного входа в систему.Контроллер задержки Ember до завершения аутентификации

Так вот сделка - когда пользователь входит в систему, они отправляются на телефонный номер. Этот.get ('session.currentUser') на контроллере приложения еще не установлен.

Когда я перехожу на другой маршрут, а затем возвращаюсь, он устанавливается правильно. Если я нахожусь на маршруте телефонных номеров после входа в систему, а затем обновляю страницу, телефонные номера загружаются правильно из-за deferReadiness и advanceReadiness в инициализаторе. Я не могу отложить проверку до входа в систему, потому что приложение уже загружено и готово.

Единственная часть, которая отсутствует, заключается в том, что после входа пользователя в систему он должен загружать числа в routes/phone-numbers.js, последний блок кода, вставленный ниже. Однако myStoreId не загружен, так как session.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); 
     }); 
    } 
    }, 
}); 

ответ

1

Попробуйте использовать посылы: предложение

// 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); 
     }); 
     } 
    }); 
    } 
}); 
+0

спасибо, я поиграю с этим. В этом примере 'this.get ('session.currentUser.id')' внутри обещания по-прежнему недоступен при входе в систему, а также при обновлении/маршрутизации он не определен и дает ошибку – awwester

1

Даниилом играть с обещаниями у меня к решению. По сути, мне нужно было дать обещание, если 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); 
     }); 
    } 
    }, 
}); 
Смежные вопросы