UNPKG

3.52 kBTypeScriptView Raw
1declare module '@ember/routing/location' {
2 /**
3 @module @ember/routing/location
4 */
5 /**
6 `Location` defines an interface to be implemented by `location` APIs. It is
7 not user-constructible; the only valid way to get a `Location` is via one of
8 its concrete implementations.
9
10 ## Implementations
11
12 You can pass an implementation name (`hash`, `history`, `none`) to force a
13 particular implementation to be used in your application.
14
15 - See [HashLocation](/ember/release/classes/HashLocation).
16 - See [HistoryLocation](/ember/release/classes/HistoryLocation).
17 - See [NoneLocation](/ember/release/classes/NoneLocation).
18
19 ## Location API
20
21 Each location implementation must provide the following methods:
22
23 * `getURL`: returns the current URL.
24 * `setURL(path)`: sets the current URL.
25 * `replaceURL(path)`: replace the current URL (optional).
26 * `onUpdateURL(callback)`: triggers the callback when the URL changes.
27 * `formatURL(url)`: formats `url` to be placed into `href` attribute.
28
29 Calling `setURL` or `replaceURL` will not trigger onUpdateURL callbacks.
30
31 ## Custom implementation
32
33 Ember scans `app/locations/*` for extending the Location API.
34
35 Example:
36
37 ```javascript
38 import HistoryLocation from '@ember/routing/history-location';
39
40 export default class MyHistory {
41 implementation = 'my-custom-history';
42
43 constructor() {
44 this._history = HistoryLocation.create(...arguments);
45 }
46
47 create() {
48 return new this(...arguments);
49 }
50
51 pushState(path) {
52 this._history.pushState(path);
53 }
54 }
55 ```
56
57 @for @ember/routing/location
58 @class Location
59 @since 5.0.0
60 @public
61 */
62 export default interface Location {
63 /**
64 * If the location needs to redirect to a different URL, it can cancel routing
65 * by setting the `cancelRouterSetup` property on itself to false.
66 * @property cancelRouterSetup
67 * @type Boolean
68 * @optional
69 * @default true
70 * @public
71 */
72 cancelRouterSetup?: boolean;
73 /**
74 * The current URL.
75 * @property currentURL
76 * @type String
77 * @public
78 */
79 getURL(): string;
80 /**
81 * Sets the current URL. Calling `setURL` will not trigger `onUpdateURL`
82 * callbacks.
83 *
84 * @property setURL
85 * @public
86 * @method
87 * @param {String} url the new URL to update to.
88 */
89 setURL(url: string): void;
90 /**
91 * Replace the current URL (optional). Calling `replaceURL` will not trigger
92 * `onUpdateURL` callbacks.
93 *
94 * @property replaceURL
95 * @public
96 * @method
97 * @param {String} url the new URL to replace the current URL with.
98 */
99 replaceURL?(url: string): void;
100 /**
101 * triggers the callback when the URL changes.
102 * @param {(newUrl: string) => void} callback A function to run when the URL
103 * changes. The the new URL string is provided as the only argument.
104 */
105 onUpdateURL(callback: UpdateCallback): void;
106 /**
107 * Formats url to be placed into href attribute.
108 *
109 * @property formatURL
110 * @public
111 * @method
112 * @param {String} url the url to format
113 */
114 formatURL(url: string): string;
115 initState?(): void;
116 destroy(): void;
117 }
118 export type UpdateCallback = (url: string) => void;
119 export interface Registry extends Record<string, Location | undefined> {}
120}