Class Ember.Route

public

The Ember.Route class is used to define individual routes. Refer to the routing guide for documentation.

Show:

Module: ember

The collection of functions, keyed by name, available on this ActionHandler as action targets.

These functions will be invoked when a matching {{action}} is triggered from within a template and the application's current route is this route.

Actions can also be invoked from other parts of your application via ActionHandler#send.

The actions hash will inherit action handlers from the actions hash defined on extended parent classes or mixins rather than just replace the entire hash, e.g.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
App.CanDisplayBanner = Ember.Mixin.create({
  actions: {
    displayBanner: function(msg) {
      // ...
    }
  }
});

App.WelcomeRoute = Ember.Route.extend(App.CanDisplayBanner, {
  actions: {
    playMusic: function() {
      // ...
    }
  }
});

// `WelcomeRoute`, when active, will be able to respond
// to both actions, since the actions hash is merged rather
// then replaced when extending mixins / parent classes.
this.send('displayBanner');
this.send('playMusic');

Within a Controller, Route, View or Component's action handler, the value of the this context is the Controller, Route, View or Component object:

1
2
3
4
5
6
7
8
9
App.SongRoute = Ember.Route.extend({
  actions: {
    myAction: function() {
      this.controllerFor("song");
      this.transitionTo("other.route");
      ...
    }
  }
});

It is also possible to call this._super(...arguments) from within an action handler if it overrides a handler defined on a parent class or mixin:

Take for example the following routes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
App.DebugRoute = Ember.Mixin.create({
  actions: {
    debugRouteInformation: function() {
      console.debug("trololo");
    }
  }
});

App.AnnoyingDebugRoute = Ember.Route.extend(App.DebugRoute, {
  actions: {
    debugRouteInformation: function() {
      // also call the debugRouteInformation of mixed in App.DebugRoute
      this._super(...arguments);

      // show additional annoyance
      window.alert(...);
    }
  }
});

Bubbling

By default, an action will stop bubbling once a handler defined on the actions hash handles it. To continue bubbling the action, you must return true from the handler:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
App.Router.map(function() {
  this.route("album", function() {
    this.route("song");
  });
});

App.AlbumRoute = Ember.Route.extend({
  actions: {
    startPlaying: function() {
    }
  }
});

App.AlbumSongRoute = Ember.Route.extend({
  actions: {
    startPlaying: function() {
      // ...

      if (actionShouldAlsoBeTriggeredOnParentRoute) {
        return true;
      }
    }
  }
});
Module: ember

Defines the properties that will be concatenated from the superclass (instead of overridden).

By default, when you extend an Ember class a property defined in the subclass overrides a property with the same name that is defined in the superclass. However, there are some cases where it is preferable to build up a property's value by combining the superclass' property value with the subclass' value. An example of this in use within Ember is the classNames property of Ember.View.

Here is some sample code showing the difference between a concatenated property and a normal one:

1
2
3
4
5
6
7
8
9
10
11
12
13
App.BarView = Ember.View.extend({
  someNonConcatenatedProperty: ['bar'],
  classNames: ['bar']
});

App.FooBarView = App.BarView.extend({
  someNonConcatenatedProperty: ['foo'],
  classNames: ['foo']
});

var fooBarView = App.FooBarView.create();
fooBarView.get('someNonConcatenatedProperty'); // ['foo']
fooBarView.get('classNames'); // ['ember-view', 'bar', 'foo']

This behavior extends to object creation as well. Continuing the above example:

1
2
3
4
5
6
var view = App.FooBarView.create({
  someNonConcatenatedProperty: ['baz'],
  classNames: ['baz']
})
view.get('someNonConcatenatedProperty'); // ['baz']
view.get('classNames'); // ['ember-view', 'bar', 'foo', 'baz']

Adding a single property that is not an array will just add it in the array:

1
2
3
4
var view = App.FooBarView.create({
  classNames: 'baz'
})
view.get('classNames'); // ['ember-view', 'bar', 'foo', 'baz']

Using the concatenatedProperties property, we can tell Ember to mix the content of the properties.

In Ember.View the classNameBindings and attributeBindings properties are also concatenated, in addition to classNames.

This feature is available for you to use throughout the Ember object model, although typical app developers are likely to use it infrequently. Since it changes expectations about behavior of properties, you should properly document its usage in each individual concatenated property (to not mislead your users to think they can override the property in a subclass).

Module: ember

Available since v1.6.0

The controller associated with this route.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
App.FormRoute = Ember.Route.extend({
  actions: {
    willTransition: function(transition) {
      if (this.controller.get('userHasEnteredData') &&
          !confirm('Are you sure you want to abandon progress?')) {
        transition.abort();
      } else {
        // Bubble the `willTransition` action so that
        // parent routes can decide whether or not to abort.
        return true;
      }
    }
  }
});
Module: ember

Available since v1.4.0

The name of the controller to associate with this route.

By default, Ember will lookup a route's controller that matches the name of the route (i.e. App.PostController for App.PostRoute). However, if you would like to define a specific controller to use, you can do so using this property.

This is useful in many ways, as the controller specified will be:

  • passed to the setupController method.
  • used as the controller for the view being rendered by the route.
  • returned from a call to controllerFor for the route.
Module: ember

Destroyed object property flag.

if this property is true the observers and bindings were already removed by the effect of calling the destroy() method.

Module: ember

Destruction scheduled flag. The destroy() method has been called.

The object stays intact until the end of the run loop at which point the isDestroyed flag is set.

Module: ember

Defines the properties that will be merged from the superclass (instead of overridden).

By default, when you extend an Ember class a property defined in the subclass overrides a property with the same name that is defined in the superclass. However, there are some cases where it is preferable to build up a property's value by merging the superclass property value with the subclass property's value. An example of this in use within Ember is the queryParams property of routes.

Here is some sample code showing the difference between a merged property and a normal one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
App.BarRoute = Ember.Route.extend({
  someNonMergedProperty: {
    nonMerged: 'superclass value of nonMerged'
  },
  queryParams: {
    page: {replace: false},
    limit: {replace: true}
  }
});

App.FooBarRoute = App.BarRoute.extend({
  someNonMergedProperty: {
    completelyNonMerged: 'subclass value of nonMerged'
  },
  queryParams: {
    limit: {replace: false}
  }
});

var fooBarRoute = App.FooBarRoute.create();

fooBarRoute.get('someNonMergedProperty');
// => { completelyNonMerged: 'subclass value of nonMerged' }
//
// Note the entire object, including the nonMerged property of
// the superclass object, has been replaced

fooBarRoute.get('queryParams');
// => {
//   page: {replace: false},
//   limit: {replace: false}
// }
//
// Note the page remains from the superclass, and the
// `limit` property's value of `false` has been merged from
// the subclass.

This behavior is not available during object create calls. It is only available at extend time.

This feature is available for you to use throughout the Ember object model, although typical app developers are likely to use it infrequently. Since it changes expectations about behavior of properties, you should properly document its usage in each individual merged property (to not mislead your users to think they can override the property in a subclass).

Module: ember

Configuration hash for this route's queryParams. The possible configuration options and their defaults are as follows (assuming a query param whose controller property is page):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
queryParams: {
  page: {
    // By default, controller query param properties don't
    // cause a full transition when they are changed, but
    // rather only cause the URL to update. Setting
    // `refreshModel` to true will cause an "in-place"
    // transition to occur, whereby the model hooks for
    // this route (and any child routes) will re-fire, allowing
    // you to reload models (e.g., from the server) using the
    // updated query param values.
    refreshModel: false,

    // By default, changes to controller query param properties
    // cause the URL to update via `pushState`, which means an
    // item will be added to the browser's history, allowing
    // you to use the back button to restore the app to the
    // previous state before the query param property was changed.
    // Setting `replace` to true will use `replaceState` (or its
    // hash location equivalent), which causes no browser history
    // item to be added. This options name and default value are
    // the same as the `link-to` helper's `replace` option.
    replace: false,

    // By default, the query param URL key is the same name as
    // the controller property name. Use `as` to specify a
    // different URL key.
    as: 'page'
  }
}
Module: ember

The name of the route, dot-delimited.

For example, a route found at app/routes/posts/post.js or app/posts/post/route.js (with pods) will have a routeName of posts.post.

Module: ember

Available since v1.4.0

The name of the template to use by default when rendering this routes template.

This is similar with viewName, but is useful when you just want a custom template without a view.

1
2
3
4
5
6
var PostsList = Ember.Route.extend({
  templateName: 'posts/list'
});

App.PostsIndexRoute = PostsList.extend();
App.PostsArchivedRoute = PostsList.extend();
Module: ember

Available since v1.4.0

The name of the view to use by default when rendering this routes template.

When rendering a template, the route will, by default, determine the template and view to use from the name of the route itself. If you need to define a specific view, set this property.

This is useful when multiple routes would benefit from using the same view because it doesn't require a custom renderTemplate method. For example, the following routes will all render using the App.PostsListView view:

1
2
3
4
5
6
var PostsList = Ember.Route.extend({
  viewName: 'postsList'
});

App.PostsIndexRoute = PostsList.extend();
App.PostsArchivedRoute = PostsList.extend();