|
| 1 | +import EmberObject from '@ember/object'; |
| 2 | +import ActionHandler from '@ember/object/-private/action-handler'; |
| 3 | +import Transition from '@ember/routing/-private/transition'; |
| 4 | +import Evented from '@ember/object/evented'; |
| 5 | +import { RenderOptions, RouteQueryParam } from '@ember/routing/types'; |
| 6 | +import Controller, { Registry as ControllerRegistry } from '@ember/controller'; |
| 7 | + |
| 8 | +/** |
| 9 | + * The `Ember.Route` class is used to define individual routes. Refer to |
| 10 | + * the [routing guide](http://emberjs.com/guides/routing/) for documentation. |
| 11 | + */ |
| 12 | +declare class Route extends EmberObject.extend(ActionHandler, Evented) { |
| 13 | + // methods |
| 14 | + /** |
| 15 | + * This hook is called after this route's model has resolved. |
| 16 | + * It follows identical async/promise semantics to `beforeModel` |
| 17 | + * but is provided the route's resolved model in addition to |
| 18 | + * the `transition`, and is therefore suited to performing |
| 19 | + * logic that can only take place after the model has already |
| 20 | + * resolved. |
| 21 | + */ |
| 22 | + afterModel(resolvedModel: any, transition: Transition): any; |
| 23 | + |
| 24 | + /** |
| 25 | + * This hook is the first of the route entry validation hooks |
| 26 | + * called when an attempt is made to transition into a route |
| 27 | + * or one of its children. It is called before `model` and |
| 28 | + * `afterModel`, and is appropriate for cases when: |
| 29 | + * 1) A decision can be made to redirect elsewhere without |
| 30 | + * needing to resolve the model first. |
| 31 | + * 2) Any async operations need to occur first before the |
| 32 | + * model is attempted to be resolved. |
| 33 | + * This hook is provided the current `transition` attempt |
| 34 | + * as a parameter, which can be used to `.abort()` the transition, |
| 35 | + * save it for a later `.retry()`, or retrieve values set |
| 36 | + * on it from a previous hook. You can also just call |
| 37 | + * `this.transitionTo` to another route to implicitly |
| 38 | + * abort the `transition`. |
| 39 | + * You can return a promise from this hook to pause the |
| 40 | + * transition until the promise resolves (or rejects). This could |
| 41 | + * be useful, for instance, for retrieving async code from |
| 42 | + * the server that is required to enter a route. |
| 43 | + */ |
| 44 | + beforeModel(transition: Transition): any; |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns the controller of the current route, or a parent (or any |
| 48 | + * ancestor) route in a route hierarchy. |
| 49 | + * |
| 50 | + * The controller instance must already have been created, either through |
| 51 | + * entering the associated route or using `generateController`. |
| 52 | + * |
| 53 | + * @param name the name of the route or controller |
| 54 | + */ |
| 55 | + controllerFor(name: string): Controller; |
| 56 | + |
| 57 | + /** |
| 58 | + * Disconnects a view that has been rendered into an outlet. |
| 59 | + */ |
| 60 | + disconnectOutlet( |
| 61 | + options: string | { outlet?: string; parentView?: string } |
| 62 | + ): void; |
| 63 | + |
| 64 | + /** |
| 65 | + * A hook you can implement to convert the URL into the model for |
| 66 | + * this route. |
| 67 | + */ |
| 68 | + model(params: {}, transition: Transition): any; |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns the model of a parent (or any ancestor) route |
| 72 | + * in a route hierarchy. During a transition, all routes |
| 73 | + * must resolve a model object, and if a route |
| 74 | + * needs access to a parent route's model in order to |
| 75 | + * resolve a model (or just reuse the model from a parent), |
| 76 | + * it can call `this.modelFor(theNameOfParentRoute)` to |
| 77 | + * retrieve it. |
| 78 | + */ |
| 79 | + modelFor(name: string): any; |
| 80 | + |
| 81 | + /** |
| 82 | + * Retrieves parameters, for current route using the state.params |
| 83 | + * variable and getQueryParamsFor, using the supplied routeName. |
| 84 | + */ |
| 85 | + paramsFor(name: string): { [k: string]: any }; |
| 86 | + |
| 87 | + /** |
| 88 | + * A hook you can implement to optionally redirect to another route. |
| 89 | + * |
| 90 | + * If you call `this.transitionTo` from inside of this hook, this route |
| 91 | + * will not be entered in favor of the other hook. |
| 92 | + * |
| 93 | + * `redirect` and `afterModel` behave very similarly and are |
| 94 | + * called almost at the same time, but they have an important |
| 95 | + * distinction in the case that, from one of these hooks, a |
| 96 | + * redirect into a child route of this route occurs: redirects |
| 97 | + * from `afterModel` essentially invalidate the current attempt |
| 98 | + * to enter this route, and will result in this route's `beforeModel`, |
| 99 | + * `model`, and `afterModel` hooks being fired again within |
| 100 | + * the new, redirecting transition. Redirects that occur within |
| 101 | + * the `redirect` hook, on the other hand, will _not_ cause |
| 102 | + * these hooks to be fired again the second time around; in |
| 103 | + * other words, by the time the `redirect` hook has been called, |
| 104 | + * both the resolved model and attempted entry into this route |
| 105 | + * are considered to be fully validated. |
| 106 | + */ |
| 107 | + redirect(model: {}, transition: Transition): void; |
| 108 | + |
| 109 | + /** |
| 110 | + * Refresh the model on this route and any child routes, firing the |
| 111 | + * `beforeModel`, `model`, and `afterModel` hooks in a similar fashion |
| 112 | + * to how routes are entered when transitioning in from other route. |
| 113 | + * The current route params (e.g. `article_id`) will be passed in |
| 114 | + * to the respective model hooks, and if a different model is returned, |
| 115 | + * `setupController` and associated route hooks will re-fire as well. |
| 116 | + * An example usage of this method is re-querying the server for the |
| 117 | + * latest information using the same parameters as when the route |
| 118 | + * was first entered. |
| 119 | + * Note that this will cause `model` hooks to fire even on routes |
| 120 | + * that were provided a model object when the route was initially |
| 121 | + * entered. |
| 122 | + */ |
| 123 | + refresh(): Transition; |
| 124 | + |
| 125 | + /** |
| 126 | + * `render` is used to render a template into a region of another template |
| 127 | + * (indicated by an `{{outlet}}`). `render` is used both during the entry |
| 128 | + * phase of routing (via the `renderTemplate` hook) and later in response to |
| 129 | + * user interaction. |
| 130 | + */ |
| 131 | + render(name: string, options?: RenderOptions): void; |
| 132 | + |
| 133 | + /** |
| 134 | + * A hook you can use to render the template for the current route. |
| 135 | + * This method is called with the controller for the current route and the |
| 136 | + * model supplied by the `model` hook. By default, it renders the route's |
| 137 | + * template, configured with the controller for the route. |
| 138 | + * This method can be overridden to set up and render additional or |
| 139 | + * alternative templates. |
| 140 | + */ |
| 141 | + renderTemplate(controller: Controller, model: {}): void; |
| 142 | + |
| 143 | + /** |
| 144 | + * Transition into another route while replacing the current URL, if possible. |
| 145 | + * This will replace the current history entry instead of adding a new one. |
| 146 | + * Beside that, it is identical to `transitionTo` in all other respects. See |
| 147 | + * 'transitionTo' for additional information regarding multiple models. |
| 148 | + */ |
| 149 | + replaceWith(name: string, ...args: any[]): Transition; |
| 150 | + |
| 151 | + /** |
| 152 | + * A hook you can use to reset controller values either when the model |
| 153 | + * changes or the route is exiting. |
| 154 | + */ |
| 155 | + resetController( |
| 156 | + controller: Controller, |
| 157 | + isExiting: boolean, |
| 158 | + transition: any |
| 159 | + ): void; |
| 160 | + |
| 161 | + /** |
| 162 | + * Sends an action to the router, which will delegate it to the currently active |
| 163 | + * route hierarchy per the bubbling rules explained under actions. |
| 164 | + */ |
| 165 | + send(name: string, ...args: any[]): void; |
| 166 | + |
| 167 | + /** |
| 168 | + * A hook you can implement to convert the route's model into parameters |
| 169 | + * for the URL. |
| 170 | + * |
| 171 | + * The default `serialize` method will insert the model's `id` into the |
| 172 | + * route's dynamic segment (in this case, `:post_id`) if the segment contains '_id'. |
| 173 | + * If the route has multiple dynamic segments or does not contain '_id', `serialize` |
| 174 | + * will return `Ember.getProperties(model, params)` |
| 175 | + * This method is called when `transitionTo` is called with a context |
| 176 | + * in order to populate the URL. |
| 177 | + */ |
| 178 | + serialize(model: {}, params: string[]): string | object; |
| 179 | + |
| 180 | + /** |
| 181 | + * A hook you can use to setup the controller for the current route. |
| 182 | + * This method is called with the controller for the current route and the |
| 183 | + * model supplied by the `model` hook. |
| 184 | + * By default, the `setupController` hook sets the `model` property of |
| 185 | + * the controller to the `model`. |
| 186 | + * If you implement the `setupController` hook in your Route, it will |
| 187 | + * prevent this default behavior. If you want to preserve that behavior |
| 188 | + * when implementing your `setupController` function, make sure to call |
| 189 | + * `_super` |
| 190 | + */ |
| 191 | + setupController(controller: Controller, model: {}): void; |
| 192 | + |
| 193 | + /** |
| 194 | + * Transition the application into another route. The route may |
| 195 | + * be either a single route or route path |
| 196 | + */ |
| 197 | + transitionTo(name: string, ...object: any[]): Transition; |
| 198 | + |
| 199 | + /** |
| 200 | + * The name of the view to use by default when rendering this routes template. |
| 201 | + * When rendering a template, the route will, by default, determine the |
| 202 | + * template and view to use from the name of the route itself. If you need to |
| 203 | + * define a specific view, set this property. |
| 204 | + * This is useful when multiple routes would benefit from using the same view |
| 205 | + * because it doesn't require a custom `renderTemplate` method. |
| 206 | + */ |
| 207 | + transitionTo(name: string, ...object: any[]): Transition; |
| 208 | + |
| 209 | + // https://emberjs.com/api/ember/3.2/classes/Route/methods/intermediateTransitionTo?anchor=intermediateTransitionTo |
| 210 | + /** |
| 211 | + * Perform a synchronous transition into another route without attempting to resolve promises, |
| 212 | + * update the URL, or abort any currently active asynchronous transitions |
| 213 | + * (i.e. regular transitions caused by transitionTo or URL changes). |
| 214 | + * |
| 215 | + * @param name the name of the route or a URL |
| 216 | + * @param object the model(s) or identifier(s) to be used while |
| 217 | + * transitioning to the route. |
| 218 | + * @returns the Transition object associated with this attempted transition |
| 219 | + */ |
| 220 | + intermediateTransitionTo(name: string, ...object: any[]): Transition; |
| 221 | + |
| 222 | + // properties |
| 223 | + /** |
| 224 | + * The controller associated with this route. |
| 225 | + */ |
| 226 | + controller: Controller; |
| 227 | + |
| 228 | + /** |
| 229 | + * The name of the controller to associate with this route. |
| 230 | + * By default, Ember will lookup a route's controller that matches the name |
| 231 | + * of the route (i.e. `App.PostController` for `App.PostRoute`). However, |
| 232 | + * if you would like to define a specific controller to use, you can do so |
| 233 | + * using this property. |
| 234 | + * This is useful in many ways, as the controller specified will be: |
| 235 | + * * p assed to the `setupController` method. |
| 236 | + * * used as the controller for the view being rendered by the route. |
| 237 | + * * returned from a call to `controllerFor` for the route. |
| 238 | + */ |
| 239 | + controllerName: string; |
| 240 | + |
| 241 | + /** |
| 242 | + * Configuration hash for this route's queryParams. |
| 243 | + */ |
| 244 | + queryParams: { [key: string]: RouteQueryParam }; |
| 245 | + |
| 246 | + /** |
| 247 | + * The name of the route, dot-delimited |
| 248 | + */ |
| 249 | + routeName: string; |
| 250 | + |
| 251 | + /** |
| 252 | + * The name of the template to use by default when rendering this routes |
| 253 | + * template. |
| 254 | + * This is similar with `viewName`, but is useful when you just want a custom |
| 255 | + * template without a view. |
| 256 | + */ |
| 257 | + templateName: string; |
| 258 | + |
| 259 | + // events |
| 260 | + /** |
| 261 | + * This hook is executed when the router enters the route. It is not executed |
| 262 | + * when the model for the route changes. |
| 263 | + */ |
| 264 | + activate(): void; |
| 265 | + |
| 266 | + /** |
| 267 | + * This hook is executed when the router completely exits this route. It is |
| 268 | + * not executed when the model for the route changes. |
| 269 | + */ |
| 270 | + deactivate(): void; |
| 271 | + |
| 272 | + /** |
| 273 | + * The didTransition action is fired after a transition has successfully been |
| 274 | + * completed. This occurs after the normal model hooks (beforeModel, model, |
| 275 | + * afterModel, setupController) have resolved. The didTransition action has |
| 276 | + * no arguments, however, it can be useful for tracking page views or resetting |
| 277 | + * state on the controller. |
| 278 | + */ |
| 279 | + didTransition(): void; |
| 280 | + |
| 281 | + /** |
| 282 | + * When attempting to transition into a route, any of the hooks may return a promise |
| 283 | + * that rejects, at which point an error action will be fired on the partially-entered |
| 284 | + * routes, allowing for per-route error handling logic, or shared error handling logic |
| 285 | + * defined on a parent route. |
| 286 | + */ |
| 287 | + error(error: any, transition: Transition): void; |
| 288 | + |
| 289 | + /** |
| 290 | + * The loading action is fired on the route when a route's model hook returns a |
| 291 | + * promise that is not already resolved. The current Transition object is the first |
| 292 | + * parameter and the route that triggered the loading event is the second parameter. |
| 293 | + */ |
| 294 | + loading(transition: Transition, route: Route): void; |
| 295 | + |
| 296 | + /** |
| 297 | + * The willTransition action is fired at the beginning of any attempted transition |
| 298 | + * with a Transition object as the sole argument. This action can be used for aborting, |
| 299 | + * redirecting, or decorating the transition from the currently active routes. |
| 300 | + */ |
| 301 | + willTransition(transition: Transition): void; |
| 302 | +} |
| 303 | + |
| 304 | +export default Route; |
0 commit comments