github.com/manicqin/nomad@v0.9.5/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  
    12    queryParams: {
    13      region: {
    14        refreshModel: true,
    15      },
    16    },
    17  
    18    resetController(controller, isExiting) {
    19      if (isExiting) {
    20        controller.set('error', null);
    21      }
    22    },
    23  
    24    beforeModel(transition) {
    25      return RSVP.all([this.get('system.regions'), this.get('system.defaultRegion')]).then(
    26        promises => {
    27          if (!this.get('system.shouldShowRegions')) return promises;
    28  
    29          const queryParam = transition.to.queryParams.region;
    30          const defaultRegion = this.get('system.defaultRegion.region');
    31          const currentRegion = this.get('system.activeRegion') || defaultRegion;
    32  
    33          // Only reset the store if the region actually changed
    34          if (
    35            (queryParam && queryParam !== currentRegion) ||
    36            (!queryParam && currentRegion !== defaultRegion)
    37          ) {
    38            this.system.reset();
    39            this.store.unloadAll();
    40          }
    41  
    42          this.set('system.activeRegion', queryParam || defaultRegion);
    43  
    44          return promises;
    45        }
    46      );
    47    },
    48  
    49    // Model is being used as a way to transfer the provided region
    50    // query param to update the controller state.
    51    model(params) {
    52      return params.region;
    53    },
    54  
    55    setupController(controller, model) {
    56      const queryParam = model;
    57  
    58      if (queryParam === this.get('system.defaultRegion.region')) {
    59        next(() => {
    60          controller.set('region', null);
    61        });
    62      }
    63  
    64      return this._super(...arguments);
    65    },
    66  
    67    actions: {
    68      didTransition() {
    69        if (!this.get('config.isTest')) {
    70          window.scrollTo(0, 0);
    71        }
    72      },
    73  
    74      willTransition() {
    75        this.controllerFor('application').set('error', null);
    76      },
    77  
    78      error(error) {
    79        if (!(error instanceof AbortError)) {
    80          this.controllerFor('application').set('error', error);
    81        }
    82      },
    83    },
    84  });