Merge pull request #2212 from jjeffery/issue-2211

#2211: Add IUrlRouter interface
This commit is contained in:
Masahiro Wakame
2014-05-21 12:24:55 +09:00
2 changed files with 53 additions and 0 deletions
+39
View File
@@ -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);
+14
View File
@@ -91,4 +91,18 @@ declare module ng.ui {
interface IStateParamsService {
[key: string]: any;
}
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;
}
}