@@ -5,17 +5,78 @@ import {isBlank} from 'angular2/src/core/facade/lang';
55import { BaseException , WrappedException } from 'angular2/src/core/facade/exceptions' ;
66import { OpaqueToken , Injectable , Optional , Inject } from 'angular2/src/core/di' ;
77
8+ /**
9+ * The `APP_BASE_HREF` token represents the base href to be used with the
10+ * {@link PathLocationStrategy}.
11+ *
12+ * If you're using {@link PathLocationStrategy}, you must provide a binding to a string
13+ * representing the URL prefix that should be preserved when generating and recognizing
14+ * URLs.
15+ *
16+ * ## Example
17+ *
18+ * ```
19+ * import {Component, View} from 'angular2/angular2';
20+ * import {ROUTER_DIRECTIVES, routerBindings, RouteConfig} from 'angular2/router';
21+ *
22+ * @Component ({...})
23+ * @View ({directives: [ROUTER_DIRECTIVES]})
24+ * @RouteConfig ([
25+ * {...},
26+ * ])
27+ * class AppCmp {
28+ * // ...
29+ * }
30+ *
31+ * bootstrap(AppCmp, [
32+ * routerBindings(AppCmp),
33+ * PathLocationStrategy,
34+ * bind(APP_BASE_HREF).toValue('/my/app')
35+ * ]);
36+ * ```
37+ */
838export const APP_BASE_HREF : OpaqueToken = CONST_EXPR ( new OpaqueToken ( 'appBaseHref' ) ) ;
939
1040/**
11- * This is the service that an application developer will directly interact with.
41+ * `Location` is a service that applications can use to interact with a browser's URL.
42+ * Depending on which {@link LocationStrategy} is used, `Location` will either persist
43+ * to the URL's path or the URL's hash segment.
44+ *
45+ * Note: it's better to use {@link Router#navigate} service to trigger route changes. Use
46+ * `Location` only if you need to interact with or create normalized URLs outside of
47+ * routing.
1248 *
13- * Responsible for normalizing the URL against the application's base href.
49+ * `Location` is responsible for normalizing the URL against the application's base href.
1450 * A normalized URL is absolute from the URL host, includes the application's base href, and has no
1551 * trailing slash:
1652 * - `/my/app/user/123` is normalized
1753 * - `my/app/user/123` **is not** normalized
1854 * - `/my/app/user/123/` **is not** normalized
55+ *
56+ * ## Example
57+ *
58+ * ```
59+ * import {Component, View} from 'angular2/angular2';
60+ * import {
61+ * ROUTER_DIRECTIVES,
62+ * routerBindings,
63+ * RouteConfig,
64+ * Location
65+ * } from 'angular2/router';
66+ *
67+ * @Component ({...})
68+ * @View ({directives: [ROUTER_DIRECTIVES]})
69+ * @RouteConfig ([
70+ * {...},
71+ * ])
72+ * class AppCmp {
73+ * constructor(location: Location) {
74+ * location.go('/foo');
75+ * }
76+ * }
77+ *
78+ * bootstrap(AppCmp, [routerBindings(AppCmp)]);
79+ * ```
1980 */
2081@Injectable ( )
2182export class Location {
@@ -36,28 +97,52 @@ export class Location {
3697 ( _ ) => { ObservableWrapper . callNext ( this . _subject , { 'url' : this . path ( ) , 'pop' : true } ) ; } ) ;
3798 }
3899
100+ /**
101+ * Returns the normalized URL path.
102+ */
39103 path ( ) : string { return this . normalize ( this . platformStrategy . path ( ) ) ; }
40104
105+ /**
106+ * Given a string representing a URL, returns the normalized URL path.
107+ */
41108 normalize ( url : string ) : string {
42109 return stripTrailingSlash ( _stripBaseHref ( this . _baseHref , stripIndexHtml ( url ) ) ) ;
43110 }
44111
112+ /**
113+ * Given a string representing a URL, returns the normalized URL path.
114+ * If the given URL doesn't begin with a leading slash (`'/'`), this method adds one
115+ * before normalizing.
116+ */
45117 normalizeAbsolutely ( url : string ) : string {
46118 if ( ! url . startsWith ( '/' ) ) {
47119 url = '/' + url ;
48120 }
49121 return stripTrailingSlash ( _addBaseHref ( this . _baseHref , url ) ) ;
50122 }
51123
124+ /**
125+ * Changes the browsers URL to the normalized version of the given URL, and pushes a
126+ * new item onto the platform's history.
127+ */
52128 go ( url : string ) : void {
53129 var finalUrl = this . normalizeAbsolutely ( url ) ;
54130 this . platformStrategy . pushState ( null , '' , finalUrl ) ;
55131 }
56132
133+ /**
134+ * Navigates forward in the platform's history.
135+ */
57136 forward ( ) : void { this . platformStrategy . forward ( ) ; }
58137
138+ /**
139+ * Navigates back in the platform's history.
140+ */
59141 back ( ) : void { this . platformStrategy . back ( ) ; }
60142
143+ /**
144+ * Subscribe to the platform's `popState` events.
145+ */
61146 subscribe ( onNext : ( value : any ) => void , onThrow : ( exception : any ) => void = null ,
62147 onReturn : ( ) => void = null ) : void {
63148 ObservableWrapper . subscribe ( this . _subject , onNext , onThrow , onReturn ) ;
0 commit comments