mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-09 11:13:57 +08:00
Merge pull request #2212 from jjeffery/issue-2211
#2211: Add IUrlRouter interface
This commit is contained in:
@@ -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);
|
||||
|
||||
Vendored
+14
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user