balderdashy / mast

UI conventions built on top of Backbone.JS
MIT License
81 stars 14 forks source link

Added ability to redirect mid-route from within a beforeRoute handler #96

Closed sgress454 closed 11 years ago

sgress454 commented 11 years ago

Mast components currently trigger "beforeRoute" and "afterRoute" events when handling a route subscription. But there's currently no way to affect the routing from within one of these handlers--the classic case being a handler wanting to check the user's login status and redirect to login screen if necessary. This update adds on object argument to the "trigger" call that the event handler can modify to alert the component that some action should occur. Currently only redirection is supported; if the object is modified to have a "redirect" property, then after all of the handlers are run, the component will Mast.navigate() to that property's value instead of continuing on the route (the afterRoute event will not be triggered).

Note that changes to the object in one beforeRoute event handler will be visible to subsequent handlers. This method could be further fleshed out to provide a full middleware layer to the routing process.

mikermcneil commented 11 years ago

Thanks! This is awesome. I need to do something similar in beforeClose as well, to give access to a callback as an argument that you can trigger when you're done and "finish off" the component.