Defining a multi segmented catch all route in ember.js
Asked Answered
C

1

9

I am using Ember.js and I would like to create a catch all route to send the user back to the root of the application if they navigate to a URL that does not match a resource. (I am using the history API) I have implemented this like so:

App.Router.map(function() {
    this.resource('things', function() {
        this.resource('thing', {path:':thing_id'}); 
    });
    this.route('catchAll', { path: ':*' });
    this.route('catchAll', { path: ':*/:*' });
    this.route('catchAll', { path: ':*/:*/:*' });
});

App.Router.reopen({
  location: 'history'
});

App.CatchAllRoute = Ember.Route.extend({ 
    redirect: function() {
        this.transitionTo('index'); 
    }
});

App.IndexRoute = Ember.Route.extend({ 

});

My question is: Can I define a single catch all route that will match any path that has not resolved to a resource irrespective of the number of segments in the path?

I am using Ember.VERSION : 1.0.0-rc.1

Cobos answered 25/2, 2013 at 13:53 Comment(0)
S
22

After some fiddling i think I've found a solution: the *: seems to do the trick, like

this.route('catchAll', { path: '*:' });

I set up this fiddle to demonstrate how it works.

Scrumptious answered 26/2, 2013 at 20:24 Comment(2)
This is correct, more or less, but a bit confusing. The : is the name you are giving to the catch all part. Rather do *wildcardurl or similar. Like when you are doing things with the params in your model setup.Westbrook
Is it just me or is this forcing a full page reload? where as a normal route wouldnt?Unsuitable

© 2022 - 2024 — McMap. All rights reserved.