Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js - Error States in Pod structure

Here is my structure in inside àpp/pods:

|-application
|-index
|-error
|-user
||-index
||-view
||-edit

When a error occurs, ember does not load the error route. Instead it tries to load a sub-route like index_error or user_error but these do not exist.

How do i force Ember to load the root error route on any error?

Ember v2.1 Ember-Cli v1.13.8

like image 528
medokin Avatar asked Oct 22 '15 10:10

medokin


People also ask

What are Ember pods?

What are Ember Pods? Ember pods are a way of structuring your project by feature, instead of type. Instead of having a directory structure with several types (controllers, models, templates...), everything is grouped around a feature (comments, posts...).

How to handle the error event in Ember?

This error event can be handled and used to display an error message, redirect to a login page, etc. In analogy with the loading event, you could manage the error event at the Application level to perform any app logic and based on the result of the last error handler, Ember will decide if substate behavior must be performed or not.

How does the Ember router handle Asynchronous transitions between routes?

In addition to the techniques described in the Asynchronous Routing Guide , the Ember Router provides powerful yet overridable conventions for customizing asynchronous transitions between routes by making use of error and loading substates.

Is there a way to use pods alongside default structure?

You can use pods alongside the default structure with ember-cli. It will first look to the pods structure, then fallback to the traditional structure if files aren't found. I haven't found a way yet. : (


Video Answer


1 Answers

The structure you've provided should actually do exactly what you're describing you're after.

Take a look at this twiddle to see an example. Clicking 'View User' will transition to the user.view route, but clicking 'Edit User' will raise an exception in the user.edit route and instead land you on the error route.

One thing to note is that you should not add the error route yourself in router.js. It receives the transition error as its model, so if you manually do this.route('error') and don't give it a dynamic segment, the transition will fail.


If you want more control over exactly what happens when an error occurs during any transition, you can implement the error action on your application route.

actions: {
  error(thrownError) {
    this.transitionTo('my-error-route'); // Or whatever other handling you want
  }
}

You can see a full example of such an arrangement in this twiddle. Note that this is subtly different from the default error behavior in that it will produce a full transition (i.e. the URL will be updated) rather than just moving into a substate.

like image 53
dfreeman Avatar answered Sep 23 '22 15:09

dfreeman