EmberJS – Router Recovering from Rejection

The promise rejects can be cached within the model hook which can be converted into fulfills that will not put the transition on halt.

Syntax

Ember.Route.extend ({
   model() {
      //return the recovery message
   }
});

Example

The example given below shows how transition will be aborted if model rejects the promise. Create a new route and name it as promisereject and open the router.js file to define URL mappings −

import Ember from 'ember';                   
//Access to Ember.js library as variable Ember
import config from './config/environment';
//It provides access to app's configuration data as variable config 

//The const declares read only variable
const Router = Ember.Router.extend ({
   location: config.locationType,
   rootURL: config.rootURL
});

//Defines URL mappings that takes parameter as an object to create the routes
Router.map(function() {
   this.route('recoveryrejection');
});

//It specifies Router variable available to other parts of the app
export default Router;

Open the application.hbs file created under app/templates/ with the following code −

<h2>Recovering from Rejection</h2>
{{#link-to 'recoveryrejection'}}Click Here{{/link-to}}

When you click the above link, it will open the recoveryrejection template page. The recoveryrejection.hbs file contains the following code −

{{model.msg}}
{{outlet}}

Now open the recoveryrejection.js file created under app/routes/ with the following code −

import Ember from 'ember';
import RSVP from 'rsvp';

export default Ember.Route.extend ({
   model() {
      //returning recovery message
      return {
         msg: "Recovered from rejected promise"
      };
   }
});

Output

Run the ember server and you will receive the following output −

 promise rejects

When you click on the link, promise will be rejected and it will display a recovery message to continue with the transition −

 promise rejects

Previous Page:-Click Here

Leave a Reply