0
votes

I am trying to do conditional transition in my router.js. I need to determine the condition(Which is eligibility) based on the boolean value returned by another rest service. Can I write a function to determine that value in router.js and how to use that value to redirect my transition to two separate routes. If the eligibity is true, I need to do ' this.route('coverage', { path: '/' }); else this.route('inEligible', { path: '/' }); ' Please provide me an example. I am very new to ember.

1
can you format your code and provide the files paths for each of the code snippets?NullVoxPopuli

1 Answers

3
votes

I use this pattern in my routes:

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';


export default class SettingsRoute extends Route {
  @service currentUser;

  async beforeModel() {
    // inside the service, I have logic for checking if the user
    // authenticated, not expired, and fetched
    const isLoggedIn = await this.currentUser.isLoggedIn();

    if (!isLoggedIn) {
      this.transitionTo('setup');
      return;
    }
  }
}

if you're using non-native classes:

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';


export default Route.extend({
  currentUser: service();

  async beforeModel() {
    // inside the service, I have logic for checking if the user
    // authenticated, not expired, and fetched
    const isLoggedIn = await this.currentUser.isLoggedIn();

    if (!isLoggedIn) {
      this.transitionTo('setup');
      return;
    }
  }
});