Last active
September 15, 2015 02:37
-
-
Save pixelhandler/8856330 to your computer and use it in GitHub Desktop.
Handle 404 with Ember Data, pseudo code: see route.js, application_adapter.js, router.js (given a response.status = 404) the router would redirect to a 404 route.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| App.ApplicationAdapter = DS.RESTAdapter.extend({ | |
| ajaxError: function(jqXHR) { | |
| var error = this._super(jqXHR), | |
| errors = {}; | |
| if (jqXHR && jqXHR.status === 422) { | |
| var jsonErrors = Ember.$.parseJSON(jqXHR.responseText)["errors"]; | |
| forEach(Ember.keys(jsonErrors), function (key) { | |
| errors[Ember.String.camelize(key)] = jsonErrors[key]; | |
| }); | |
| return new DS.InvalidError(errors); | |
| } else if (jqXHR && jqXHR.status === 404) { | |
| errors['404'] = 'Not Found'; | |
| return new DS.InvalidError(errors); | |
| } else { | |
| return error; | |
| } | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| App.ThingDetailRoute = Em.Route.extend({ | |
| model: function(params, transition) { | |
| return this.store.find('thing', params.thing_id).then( | |
| (function(_this) { | |
| return function(model) { | |
| return _this.store.push('thing', model); | |
| }; | |
| })(this), | |
| (function(_this) { | |
| return function(invalid) { | |
| if (invalid.errors && invalid.errors['404']) { | |
| return _this.transitionTo('not-found', invalid); | |
| } | |
| }; | |
| })(this)); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| App.Router.map(function() { | |
| this.resource('thing', {path: '/thing'}, function() { | |
| this.route('detail', {path: '/:thing_id'}); | |
| }); | |
| this.route('not-found', {path: '/*path'}); | |
| }); |
@shadow-fox yeah that is an option too: http://emberjs.com/guides/routing/loading-and-error-substates/#toc_code-error-code-substates ; could use a specific ErrorRoute too
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't you handle the error part in the route's action hooks already baked error action ?