Working with asynchronous routing

A more advanced feature of the router is dealing with asynchronous logic. The following recipes explain this concept using promises.

How to do it...

In the route, Ember makes heavy use of promises. Promises are objects that represent an eventual value. We can use promises in our model.

  1. Create a new router for the application route:
    $ ember g route application
    

    If prompted to overwrite the template, type Y. This will generate the router file for the default application route.

  2. Add a new model to the application.js file in the app/router folder:
    // app/router/application.js import Ember from 'ember'; export default Ember.Route.extend({ model() { return new Ember.RSVP.Promise(function(resolve) { resolve({message: 'Resolved'}); ...

Get Ember.js Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.