%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
Architecture, Auth, and Routing with uiRouter
1. ARCHITECTURE, AUTH, AND ROUTING WITH UIROUTER
chris caplinger
ccaplinger@neosavvy.com
@unicode_snowman
2. WHAT IS UIROUTER?
“AngularUI Router is a routing framework for
AngularJS, which allows you to organize the
parts of your interface into a state machine.
Unlike the $route service in the Angular ngRoute
module, which is organized around URL routes,
UI-Router is organized around states, which may
optionally be associated with URLs.”
3. WHY A ROUTER IN GENERAL?
Associates Routes/URLs with application state
i.e. views and their associated controllers and models
4. “When we wrote the original router it was, to our
imagination, pretty cool... except it was vastly
inadequate for a large number of use cases.”
5. NGROUTE SHORTCOMINGS
only one ngView
encourages ngShow/ngHide/ngIf/ngSwitch abuse
nested templates in nested templates in nested templates... in nested templates..
...overall, painful with larger apps
8. STATE STATE STATE
“...an abstract machine that can be in one of a
finite number of states. The machine is in only
one state at a time... It can transition from one
state to another by a triggering event or
condition; A particular FSM is defined by a list of
its states, and the triggering condition for each
transition.”
15. // get there with...
$state.go('items.detail', { id: 123 });
// or relative...
$state.go('.detail', { id: 123 });
<!-- or via a link... -->
<a ui-sref=".detail({ id: 123 })">Item 123</a>
21. OBLIGATORY OBVIOUS
THING
CLIENT-SIDE AUTH AIN'T NO THING
server side is where the magic happens
this is really about UX
22. $STATECHANGE* EVENTS
angular.app('MyApp').run(function ($rootScope, $state) {
$rootScope.$on('$stateChangeError', function (e, toState, toParams, fromState, fromParams, if (error.status == 401) {
$state.go('login');
} else {
// other error types, 404, 500
// maybe a redirect, maybe not
}
});
});
23. angular.app('MyApp').run(function ($rootScope, $state, AuthService, User) {
$rootScope.$on('$stateChangeStart', function (e, toState, toParams, fromState, fromParams, if (toState.data.checkAuth && !User.isAuthorized) {
e.preventDefault();
AuthService.doAsyncThing().then(function (res) {
$state.go(toState, toParams, { notify: false }).then(function () {
$rootScope.$broadcast('$stateChangeSuccess', toState, toParams, fromState
});
}).catch(function (err) {
// do something here, redirect, or let your $stateChangeError handler catch })
}
});
});
24. OTHER THOUGHTS ON REDIRECTION
decorate $state.transitionTo for finer-grained control (a bit
hackier than I'd like, but it works)
$stateChangeError redirection from navigating directly to a
URL, need to replace entry