cyclomarc
cyclomarc

Reputation: 2012

Ember: how to handle unexpected routes?

The router nicely treats the URL and results in calling the appropriate route if the route exists. But how to handle the case where the route does not exist ?

app.js

App = Ember.Application.create();

App.Router.map(function () {
  this.resource('about', { path: "/about" });
  this.resource('admin', { path: "/admin" });
});

index.html

<script type="text/x-handlebars" id="about">
      <h1>ABOUT</h1>
</script>

<script type="text/x-handlebars" id="admin">
      <h1>ADMIN</h1>
</script>

Use case:

When a user enters the URL index.html#/xxxx, I would like to transition to a sort of error page indicating that the requested "page" does not exit.

So far, I did not found a solution ...

Upvotes: 2

Views: 1212

Answers (2)

mlienau
mlienau

Reputation: 813

Ember.JS: How to handle invalid URLs is a good example if you're looking for another approach.

Upvotes: 3

mavilein
mavilein

Reputation: 11668

You can add a catch all Route like this:

this.route('missing', { path: "/*path" });

Then redirect within this route. My implementation looks like the following:

App.MissingRoute = Ember.Route.extend({
    redirect : function(){
        App.LOG.warn("No Route for given URL found. Will transition to Index Route instead.");
        this.transitionTo("index");
    }
});

Upvotes: 8

Related Questions