ember.js - Ember Router - How to handle 404 (Not Found) routes? -


i'm trying figure out how handle invalid routes within application using ember.router.

currently if enter invalid route, e.g. myapp.com/#foobardoesntexist, redirect index route ('/'). i'd if define notfound or 404 state route can inform user happend. opposed them getting dumped on home page.

ember.router in current version not provide means handle unknown routes. time hack!

solution 1 - quick , dirty

the idea here following. have ember.router.route(path) method, invoked requested (potentially unknown) path. after invocation of method, path of router guaranteed known. so, if compare requested path , actual path , differ - requested path invalid , may redirect user 404 page.

  app.router = ember.router.extend({      route: function(path) {       this._super(path);       var actualpath = this.get("currentstate").absoluteroute(this);       if (path !== actualpath) {         this.transitionto("404page");       }     }   }); 

this solution quite expensive. example, if current state "/a/b/c", , user wants navigate "/b/d/e/unknown", router dutifully enter known states "b", "d" , "e", , discard path unknown. nice if can tell before actual routing starts.

solution 2 - fiddling private methods

here check validity of given path, , tell router proceed:

app.router = ember.router.extend({  checkpath: function (path) {   path = path.replace(this.get('rooturl'), '').replace(/^(?=[^\/])/, "/");    var resolvedstates = this.get("states.root").resolvepath(this, path);   var laststate = resolvedstates.get("lastobject");   return laststate.match.remaining == ""; },  route: function(path) {   if (this.checkpath(path)) {     this._super(path);   } else {     this.transitionto("404page");   } } }); 

this solution has drawback - uses resolvepath method marked private. nevertheless, i'd use solution, since more effective first one.


Comments

Post a Comment

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Socket.connect doesn't throw exception in Android -

SPSS keyboard combination alters encoding -