W3cubDocs

/Ember.js

Ember.Route (public)

Ember.ActionHandler
Extends: Ember.Object
Uses: Ember.Evented ,
Defined in: packages/ember-routing/lib/system/route.js:66
Module: ember

activate public

This event is triggered when the router enters the route. It is not executed when the model for the route changes.

import Ember from 'ember';

export default Ember.Route.extend({
  collectAnalytics: Ember.on('activate', function(){
    collectAnalytics();
  })
});

deactivate public

This event is triggered when the router completely exits this route. It is not executed when the model for the route changes.

import Ember from 'ember';

export default Ember.Route.extend({
  trackPageLeaveAnalytics: Ember.on('deactivate', function(){
    trackPageLeaveAnalytics();
  })
});

didTransition public

The didTransition action is fired after a transition has successfully been completed. This occurs after the normal model hooks (beforeModel, model, afterModel, setupController) have resolved. The didTransition action has no arguments, however, it can be useful for tracking page views or resetting state on the controller.

import Ember from 'ember';

export default Ember.Route.extend({
  actions: {
    didTransition() {
      this.controller.get('errors.base').clear();
      return true; // Bubble the didTransition event
    }
  }
});

error (error, transition) public

error
Error
transition
Transition

When attempting to transition into a route, any of the hooks may return a promise that rejects, at which point an error action will be fired on the partially-entered routes, allowing for per-route error handling logic, or shared error handling logic defined on a parent route.

Here is an example of an error handler that will be invoked for rejected promises from the various hooks on the route, as well as any unhandled errors from child routes:

import Ember from 'ember';

export default Ember.Route.extend({
  beforeModel() {
    return Ember.RSVP.reject('bad things!');
  },

  actions: {
    error(error, transition) {
      // Assuming we got here due to the error in `beforeModel`,
      // we can expect that error === "bad things!",
      // but a promise model rejecting would also
      // call this hook, as would any errors encountered
      // in `afterModel`.

      // The `error` hook is also provided the failed
      // `transition`, which can be stored and later
      // `.retry()`d if desired.

      this.transitionTo('login');
    }
  }
});

error actions that bubble up all the way to ApplicationRoute will fire a default error handler that logs the error. You can specify your own global default error handler by overriding the error handler on ApplicationRoute:

import Ember from 'ember';

export default Ember.Route.extend({
  actions: {
    error(error, transition) {
      this.controllerFor('banner').displayError(error.message);
    }
  }
});

loading (transition, route) public

transition
Transition
route
Ember.Route
The route that triggered the loading event

The loading action is fired on the route when a route's model hook returns a promise that is not already resolved. The current Transition object is the first parameter and the route that triggered the loading event is the second parameter.

export default Ember.Route.extend({
  actions: {
    loading(transition, route) {
      let controller = this.controllerFor('foo');
      controller.set('currentlyLoading', true);

      transition.finally(function() {
        controller.set('currentlyLoading', false);
      });
    }
  }
});

willTransition (transition) public

transition
Transition

The willTransition action is fired at the beginning of any attempted transition with a Transition object as the sole argument. This action can be used for aborting, redirecting, or decorating the transition from the currently active routes.

A good example is preventing navigation when a form is half-filled out:

import Ember from 'ember';

export default Ember.Route.extend({
  actions: {
    willTransition(transition) {
      if (this.controller.get('userHasEnteredData')) {
        this.controller.displayNavigationConfirm();
        transition.abort();
      }
    }
  }
});

You can also redirect elsewhere by calling this.transitionTo('elsewhere') from within willTransition. Note that willTransition will not be fired for the redirecting transitionTo, since willTransition doesn't fire when there is already a transition underway. If you want subsequent willTransition actions to fire for the redirecting transition, you must first explicitly call transition.abort().

To allow the willTransition event to continue bubbling to the parent route, use return true;. When the willTransition method has a return value of true then the parent route's willTransition method will be fired, enabling "bubbling" behavior for the event.

© 2017 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://emberjs.com/api/ember/2.15/classes/Ember.Route/events