jeudi 16 mars 2017

Ember - observer in previous route fires when property set in didTransition hook of next route

In my index route, I have an observer that fires an action when the user object is set in the session service, as shown below. This works fine- the console logs 'index route observer fired' and the action fires.

routes/index

session: Ember.inject.service(),
  sendTheAction: function() {
    console.log('index route observer fired');
    this.send('checkLicense');
  }.observes('session.user'),

  actions: {
    checkLicense: function() {
      if (this.get('session.user.email)) {
          //get the user's email and send off an AJAX request.
        }
      },
    }

I also have a logout route which (among other things) sets session.user to an empty object, and then transitions to the login route. I simply use a link-to helper pointing to 'logout' to initiate this.

routes/logout

session: Ember.inject.service(),
  actions: {
    didTransition: function() {
      this.set('session.user', {});
      this.transitionTo('login');
    }
  }

If I am in the index route and I click logout, the transition to logout begins. Then, when session.user is set to {} in the logout route's didTransition hook, the observer in the index route fires. The console logs 'index route observer fired' but then I get a console error saying

Error while processing route: logout Nothing handled the action 'checkLicense'.

I'm not sure how the observer in the previous route can still be fired by a change in the logout route's didTransition hook, as I thought didTransition only fired when the transition was fully resolved.

I think that it is then looking for the action "checkLicense" in the logout route.

Can anyone clarify why that observer still fires, and how to get around this?




Aucun commentaire:

Enregistrer un commentaire