mardi 16 janvier 2018

emberjs | beforeModel hook is firing after model hook

I have a mixin with following definition.

import Ember from 'ember';

export default Ember.Mixin.create({

  loggedinUser: {},
  beforeModel() {  

    if (!this.get('AuthService').isAuthenticated()) {
       this.transitionTo('login');
    }

    this.get('AuthService').getLoggedinUser()
    .then(results => {this.loggedinUser = results;
      alert("in before model"); 
    alert(this.loggedinUser.requestorId);    
    });     
  } }
});

I import it in my route as following

import UnAuthenticatedRouteMixin from '../mixins/un-authenticated-route-mixin';

export default Ember.Route.extend(UnAuthenticatedRouteMixin, {

  model(){  
    this._super(...arguments);

    alert("in model");
    alert(this.loggedinUser.requestorId); 

  }
});

My expected order of alerts is "in before model " -> "1234", "in model" , "1234". However the actual order of alerts is "in model " -> "undefined", "in before model" , "1234"

How can I ensure that beforeModel hook is fired after model hook.




Aucun commentaire:

Enregistrer un commentaire