github.com/thomasobenaus/nomad@v0.11.1/ui/app/routes/application.js (about)

     1  import { inject as service } from '@ember/service';
     2  import { next } from '@ember/runloop';
     3  import Route from '@ember/routing/route';
     4  import { AbortError } from '@ember-data/adapter/error';
     5  import RSVP from 'rsvp';
     6  
     7  export default Route.extend({
     8    config: service(),
     9    system: service(),
    10    store: service(),
    11    token: service(),
    12  
    13    queryParams: {
    14      region: {
    15        refreshModel: true,
    16      },
    17    },
    18  
    19    resetController(controller, isExiting) {
    20      if (isExiting) {
    21        controller.set('error', null);
    22      }
    23    },
    24  
    25    beforeModel(transition) {
    26      const fetchSelfTokenAndPolicies = this.get('token.fetchSelfTokenAndPolicies')
    27        .perform()
    28        .catch();
    29  
    30      return RSVP.all([
    31        this.get('system.regions'),
    32        this.get('system.defaultRegion'),
    33        fetchSelfTokenAndPolicies,
    34      ]).then(promises => {
    35        if (!this.get('system.shouldShowRegions')) return promises;
    36  
    37        const queryParam = transition.to.queryParams.region;
    38        const defaultRegion = this.get('system.defaultRegion.region');
    39        const currentRegion = this.get('system.activeRegion') || defaultRegion;
    40  
    41        // Only reset the store if the region actually changed
    42        if (
    43          (queryParam && queryParam !== currentRegion) ||
    44          (!queryParam && currentRegion !== defaultRegion)
    45        ) {
    46          this.system.reset();
    47          this.store.unloadAll();
    48        }
    49  
    50        this.set('system.activeRegion', queryParam || defaultRegion);
    51  
    52        return promises;
    53      });
    54    },
    55  
    56    // Model is being used as a way to transfer the provided region
    57    // query param to update the controller state.
    58    model(params) {
    59      return params.region;
    60    },
    61  
    62    setupController(controller, model) {
    63      const queryParam = model;
    64  
    65      if (queryParam === this.get('system.defaultRegion.region')) {
    66        next(() => {
    67          controller.set('region', null);
    68        });
    69      }
    70  
    71      return this._super(...arguments);
    72    },
    73  
    74    actions: {
    75      didTransition() {
    76        if (!this.get('config.isTest')) {
    77          window.scrollTo(0, 0);
    78        }
    79      },
    80  
    81      willTransition() {
    82        this.controllerFor('application').set('error', null);
    83      },
    84  
    85      error(error) {
    86        if (!(error instanceof AbortError)) {
    87          this.controllerFor('application').set('error', error);
    88        }
    89      },
    90    },
    91  });