From 7929e622e67c347d3c0a7f2af47521ab67948f5a Mon Sep 17 00:00:00 2001 From: John Jeffery Date: Mon, 19 May 2014 10:07:47 +1000 Subject: [PATCH 1/4] #2211: Add IUrlRouter interface for --- angular-ui/angular-ui-router.d.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/angular-ui/angular-ui-router.d.ts b/angular-ui/angular-ui-router.d.ts index 82bd14f38..f927ec6f7 100644 --- a/angular-ui/angular-ui-router.d.ts +++ b/angular-ui/angular-ui-router.d.ts @@ -91,4 +91,8 @@ declare module ng.ui { interface IStateParamsService { [key: string]: any; } + + interface IUrlRouter { + sync(): void; + } } From 3ffc17511941ca5a760606ddf783128aae47b207 Mon Sep 17 00:00:00 2001 From: John Jeffery Date: Wed, 21 May 2014 10:08:03 +1000 Subject: [PATCH 2/4] Rename IUrlRouter to IUrlRouterService More consistent with naming conventions for other services. --- angular-ui/angular-ui-router.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/angular-ui/angular-ui-router.d.ts b/angular-ui/angular-ui-router.d.ts index f927ec6f7..6e78e045b 100644 --- a/angular-ui/angular-ui-router.d.ts +++ b/angular-ui/angular-ui-router.d.ts @@ -92,7 +92,7 @@ declare module ng.ui { [key: string]: any; } - interface IUrlRouter { + interface IUrlRouterService { sync(): void; } } From 2d444c9c93898cd50e3df58f262aae340ff99031 Mon Sep 17 00:00:00 2001 From: John Jeffery Date: Wed, 21 May 2014 10:08:37 +1000 Subject: [PATCH 3/4] Add jsdoc for IUrlRouterService. --- angular-ui/angular-ui-router.d.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/angular-ui/angular-ui-router.d.ts b/angular-ui/angular-ui-router.d.ts index 6e78e045b..7db3f399c 100644 --- a/angular-ui/angular-ui-router.d.ts +++ b/angular-ui/angular-ui-router.d.ts @@ -93,6 +93,16 @@ declare module ng.ui { } interface IUrlRouterService { + /* + * Triggers an update; the same update that happens when the address bar + * url changes, aka $locationChangeSuccess. + * + * This method is useful when you need to use preventDefault() on the + * $locationChangeSuccess event, perform some custom logic (route protection, + * auth, config, redirection, etc) and then finally proceed with the transition + * by calling $urlRouter.sync(). + * + */ sync(): void; } } From b92a26e5d39c480f24d3e2b04b1af18cb550e952 Mon Sep 17 00:00:00 2001 From: John Jeffery Date: Wed, 21 May 2014 10:08:53 +1000 Subject: [PATCH 4/4] Add test for IUrlRouterService --- angular-ui/angular-ui-router-tests.ts | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/angular-ui/angular-ui-router-tests.ts b/angular-ui/angular-ui-router-tests.ts index 5a9c4ad6c..d7dbe7183 100644 --- a/angular-ui/angular-ui-router-tests.ts +++ b/angular-ui/angular-ui-router-tests.ts @@ -71,3 +71,42 @@ myApp.config(( } }); }); + +interface IUrlLocatorTestService { + currentUser: any; +} + +// Service for determining who the currently logged on user is. +class UrlLocatorTestService implements IUrlLocatorTestService { + static $inject = ["$http", "$rootScope", "$urlRouter"]; + + constructor( + private $http: ng.IHttpService, + private $rootScope: ng.IRootScopeService, + private $urlRouter: ng.ui.IUrlRouterService + ) { + $rootScope.$on("$locationChangeSuccess", (event: ng.IAngularEvent) => this.onLocationChangeSuccess(event)); + } + + public currentUser: any; + + private onLocationChangeSuccess(event: ng.IAngularEvent) { + if (!this.currentUser) { + // If the current user is unknown, halt the state change and request current + // user details from the server + event.preventDefault(); + + // Note that we do not concern ourselves with what to do if this request fails, + // because if it fails, the web page will be redirected away to the login screen. + this.$http({ url: "/api/me", method: "GET" }).success((user: any) => { + this.currentUser = user; + + // sync the ui-state with the location in the browser, which effectively + // restarts the state change that was stopped previously + this.$urlRouter.sync(); + }); + } + } +} + +myApp.service("urlLocatorTest", UrlLocatorTestService);