vendredi 6 mars 2020

Handle incorrect path in an EmberJS route

If an invalid customer_id is entered, I'd like the user to be redirected to 404. I cannot handle it in the route.js, since the adapter error is breaking the application even before the model() {} is visited.

router.js:

this.route('profile_details', { path: '/profile_details/:customer_id' });

route.js:

import AuthenticatedRoute from 'employer/mixins/authenticated-route';
import ApplicationRoute from 'employer/application/route';
import { inject as service } from '@ember/service';

export default ApplicationRoute.extend(AuthenticatedRoute, {
  router: service(),

  model() {
    const { customer_id } = this.paramsFor('profile-details');
    return this.store.findRecord('customer', customer_id).catch(()=> {
      this.router.transitionTo('404');
    });
  },
});

Is there a way I could handle that in the application.js adapter?

import DS from 'ember-data';
import ApplicationAdapterMixin from 'common/mixins/application-adapter';

const { RESTAdapter } = DS;

export default RESTAdapter.extend(ApplicationAdapterMixin, {
  handleResponse(status, headers, payload, requestData) {
    if(status === 404) {
      //Can I use the router service inside an adapter to transition?
    }
  }
});



Aucun commentaire:

Enregistrer un commentaire