1 | declare module '@ember/routing/route' {
|
2 | import type Owner from '@ember/owner';
|
3 | import type { default as BucketCache } from '@ember/routing/lib/cache';
|
4 | import EmberObject from '@ember/object';
|
5 | import Evented from '@ember/object/evented';
|
6 | import { ActionHandler } from '@ember/-internals/runtime';
|
7 | import type { AnyFn } from '@ember/-internals/utility-types';
|
8 | import Controller from '@ember/controller';
|
9 | import type { RenderState } from '@ember/-internals/glimmer';
|
10 | import type { InternalRouteInfo, Route as IRoute, Transition, TransitionState } from 'router_js';
|
11 | import type { QueryParam, default as EmberRouter } from '@ember/routing/router';
|
12 | import type { NamedRouteArgs } from '@ember/routing/lib/utils';
|
13 | export interface ExtendedInternalRouteInfo<R extends Route> extends InternalRouteInfo<R> {
|
14 | _names?: unknown[];
|
15 | }
|
16 | export type QueryParamMeta = {
|
17 | qps: QueryParam[];
|
18 | map: Record<string, QueryParam>;
|
19 | propertyNames: string[];
|
20 | states: {
|
21 | inactive(prop: string, value: unknown): void;
|
22 | active(prop: string, value: unknown): any;
|
23 | allowOverrides(prop: string, value: unknown): any;
|
24 | };
|
25 | };
|
26 | type RouteTransitionState = TransitionState<Route> & {
|
27 | fullQueryParams?: Record<string, unknown>;
|
28 | queryParamsFor?: Record<string, Record<string, unknown>>;
|
29 | };
|
30 | type MaybeParameters<T> = T extends AnyFn ? Parameters<T> : unknown[];
|
31 | type MaybeReturnType<T> = T extends AnyFn ? ReturnType<T> : unknown;
|
32 | const RENDER: unique symbol;
|
33 | const RENDER_STATE: unique symbol;
|
34 | |
35 |
|
36 |
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | interface Route<Model = unknown> extends IRoute<Model>, ActionHandler, Evented {
|
49 | |
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | willTransition?(transition: Transition): boolean | void;
|
93 | |
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | didTransition?(): boolean | void;
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | loading?(transition: Transition, route: Route): boolean | void;
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | error?(error: Error, transition: Transition): boolean | void;
|
217 | }
|
218 | const Route_base: Readonly<typeof EmberObject> &
|
219 | (new (owner?: Owner | undefined) => EmberObject) &
|
220 | import('@ember/object/mixin').default;
|
221 | class Route<Model = unknown> extends Route_base implements IRoute {
|
222 | static isRouteFactory: boolean;
|
223 |
|
224 | context: Model;
|
225 |
|
226 | currentModel: Model;
|
227 |
|
228 | _bucketCache: BucketCache;
|
229 |
|
230 | _internalName: string;
|
231 | private _names;
|
232 | _router: EmberRouter;
|
233 | _topLevelViewTemplate: any;
|
234 | _environment: any;
|
235 | constructor(owner?: Owner);
|
236 | /**
|
237 | A hook you can implement to convert the route's model into parameters
|
238 | for the URL.
|
239 |
|
240 | ```app/router.js
|
241 | // ...
|
242 |
|
243 | Router.map(function() {
|
244 | this.route('post', { path: '/posts/:post_id' });
|
245 | });
|
246 |
|
247 | ```
|
248 |
|
249 | ```app/routes/post.js
|
250 | import Route from '@ember/routing/route';
|
251 |
|
252 | export default class PostRoute extends Route {
|
253 | model({ post_id }) {
|
254 |
|
255 | return fetch(`/posts/${post_id}`;
|
256 | }
|
257 |
|
258 | serialize(model) {
|
259 |
|
260 | return { post_id: model.id };
|
261 | }
|
262 | }
|
263 | ```
|
264 |
|
265 | The default `serialize` method will insert the model's `id` into the
|
266 | route's dynamic segment (in this case, `:post_id`) if the segment contains '_id'.
|
267 | If the route has multiple dynamic segments or does not contain '_id', `serialize`
|
268 | will return `getProperties(model, params)`
|
269 |
|
270 | This method is called when `transitionTo` is called with a context
|
271 | in order to populate the URL.
|
272 |
|
273 | @method serialize
|
274 | @param {Object} model the routes model
|
275 | @param {Array} params an Array of parameter names for the current
|
276 | route (in the example, `['post_id']`.
|
277 | @return {Object} the serialized parameters
|
278 | @since 1.0.0
|
279 | @public
|
280 | */
|
281 | serialize(
|
282 | model: Model,
|
283 | params: string[]
|
284 | ):
|
285 | | {
|
286 | [key: string]: unknown;
|
287 | }
|
288 | | undefined;
|
289 | /**
|
290 | Configuration hash for this route's queryParams. The possible
|
291 | configuration options and their defaults are as follows
|
292 | (assuming a query param whose controller property is `page`):
|
293 |
|
294 | ```javascript
|
295 | queryParams = {
|
296 | page: {
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 | refreshModel: false,
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | replace: false,
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | as: 'page'
|
322 | }
|
323 | };
|
324 | ```
|
325 |
|
326 | @property queryParams
|
327 | @for Route
|
328 | @type Object
|
329 | @since 1.6.0
|
330 | @public
|
331 | */
|
332 | queryParams: Record<
|
333 | string,
|
334 | {
|
335 | refreshModel?: boolean;
|
336 | replace?: boolean;
|
337 | as?: string;
|
338 | }
|
339 | >;
|
340 | /**
|
341 | The name of the template to use by default when rendering this route's
|
342 | template.
|
343 |
|
344 | ```app/routes/posts/list.js
|
345 | import Route from '@ember/routing/route';
|
346 |
|
347 | export default class PostsListRoute extends Route {
|
348 | templateName = 'posts/list';
|
349 | }
|
350 | ```
|
351 |
|
352 | ```app/routes/posts/index.js
|
353 | import PostsListRoute from '../posts/list';
|
354 |
|
355 | export default class PostsIndexRoute extends PostsListRoute {};
|
356 | ```
|
357 |
|
358 | ```app/routes/posts/archived.js
|
359 | import PostsListRoute from '../posts/list';
|
360 |
|
361 | export default class PostsArchivedRoute extends PostsListRoute {};
|
362 | ```
|
363 |
|
364 | @property templateName
|
365 | @type String
|
366 | @default null
|
367 | @since 1.4.0
|
368 | @public
|
369 | */
|
370 | templateName: string | null;
|
371 | /**
|
372 | The name of the controller to associate with this route.
|
373 |
|
374 | By default, Ember will lookup a route's controller that matches the name
|
375 | of the route (i.e. `posts.new`). However,
|
376 | if you would like to define a specific controller to use, you can do so
|
377 | using this property.
|
378 |
|
379 | This is useful in many ways, as the controller specified will be:
|
380 |
|
381 | * passed to the `setupController` method.
|
382 | * used as the controller for the template being rendered by the route.
|
383 | * returned from a call to `controllerFor` for the route.
|
384 |
|
385 | @property controllerName
|
386 | @type String
|
387 | @default null
|
388 | @since 1.4.0
|
389 | @public
|
390 | */
|
391 | controllerName: string | null;
|
392 | /**
|
393 | The controller associated with this route.
|
394 |
|
395 | Example
|
396 |
|
397 | ```app/routes/form.js
|
398 | import Route from '@ember/routing/route';
|
399 | import { action } from '@ember/object';
|
400 |
|
401 | export default class FormRoute extends Route {
|
402 | @action
|
403 | willTransition(transition) {
|
404 | if (this.controller.get('userHasEnteredData') &&
|
405 | !confirm('Are you sure you want to abandon progress?')) {
|
406 | transition.abort();
|
407 | } else {
|
408 |
|
409 |
|
410 | return true;
|
411 | }
|
412 | }
|
413 | }
|
414 | ```
|
415 |
|
416 | @property controller
|
417 | @type Controller
|
418 | @since 1.6.0
|
419 | @public
|
420 | */
|
421 | controller: Controller;
|
422 | /**
|
423 | The name of the route, dot-delimited.
|
424 |
|
425 | For example, a route found at `app/routes/posts/post.js` will have
|
426 | a `routeName` of `posts.post`.
|
427 |
|
428 | @property routeName
|
429 | @for Route
|
430 | @type String
|
431 | @since 1.0.0
|
432 | @public
|
433 | */
|
434 | routeName: string;
|
435 | /**
|
436 | The name of the route, dot-delimited, including the engine prefix
|
437 | if applicable.
|
438 |
|
439 | For example, a route found at `addon/routes/posts/post.js` within an
|
440 | engine named `admin` will have a `fullRouteName` of `admin.posts.post`.
|
441 |
|
442 | @property fullRouteName
|
443 | @for Route
|
444 | @type String
|
445 | @since 2.10.0
|
446 | @public
|
447 | */
|
448 | fullRouteName: string;
|
449 | /**
|
450 | Sets the name for this route, including a fully resolved name for routes
|
451 | inside engines.
|
452 |
|
453 | @private
|
454 | @method _setRouteName
|
455 | @param {String} name
|
456 | */
|
457 | _setRouteName(name: string): void;
|
458 | /**
|
459 | @private
|
460 |
|
461 | @method _stashNames
|
462 | */
|
463 | _stashNames(
|
464 | routeInfo: ExtendedInternalRouteInfo<this>,
|
465 | dynamicParent: ExtendedInternalRouteInfo<this>
|
466 | ): void;
|
467 | /**
|
468 | @private
|
469 |
|
470 | @property _activeQPChanged
|
471 | */
|
472 | _activeQPChanged(qp: QueryParam, value: unknown): void;
|
473 | /**
|
474 | @private
|
475 | @method _updatingQPChanged
|
476 | */
|
477 | _updatingQPChanged(qp: QueryParam): void;
|
478 | /**
|
479 | Returns a hash containing the parameters of an ancestor route.
|
480 |
|
481 | You may notice that `this.paramsFor` sometimes works when referring to a
|
482 | child route, but this behavior should not be relied upon as only ancestor
|
483 | routes are certain to be loaded in time.
|
484 |
|
485 | Example
|
486 |
|
487 | ```app/router.js
|
488 |
|
489 |
|
490 | Router.map(function() {
|
491 | this.route('member', { path: ':name' }, function() {
|
492 | this.route('interest', { path: ':interest' });
|
493 | });
|
494 | });
|
495 | ```
|
496 |
|
497 | ```app/routes/member.js
|
498 | import Route from '@ember/routing/route';
|
499 |
|
500 | export default class MemberRoute extends Route {
|
501 | queryParams = {
|
502 | memberQp: { refreshModel: true }
|
503 | }
|
504 | }
|
505 | ```
|
506 |
|
507 | ```app/routes/member/interest.js
|
508 | import Route from '@ember/routing/route';
|
509 |
|
510 | export default class MemberInterestRoute extends Route {
|
511 | queryParams = {
|
512 | interestQp: { refreshModel: true }
|
513 | }
|
514 |
|
515 | model() {
|
516 | return this.paramsFor('member');
|
517 | }
|
518 | }
|
519 | ```
|
520 |
|
521 | If we visit `/turing/maths?memberQp=member&interestQp=interest` the model for
|
522 | the `member.interest` route is a hash with:
|
523 |
|
524 | * `name`: `turing`
|
525 | * `memberQp`: `member`
|
526 |
|
527 | @method paramsFor
|
528 | @param {String} name
|
529 | @return {Object} hash containing the parameters of the route `name`
|
530 | @since 1.4.0
|
531 | @public
|
532 | */
|
533 | paramsFor(name: string): Record<string, unknown>;
|
534 | /**
|
535 | Serializes the query parameter key
|
536 |
|
537 | @method serializeQueryParamKey
|
538 | @param {String} controllerPropertyName
|
539 | @private
|
540 | */
|
541 | serializeQueryParamKey(controllerPropertyName: string): string;
|
542 | /**
|
543 | Serializes value of the query parameter based on defaultValueType
|
544 |
|
545 | @method serializeQueryParam
|
546 | @param {Object} value
|
547 | @param {String} urlKey
|
548 | @param {String} defaultValueType
|
549 | @private
|
550 | */
|
551 | serializeQueryParam(
|
552 | value: unknown,
|
553 | _urlKey: string,
|
554 | defaultValueType: string
|
555 | ): string | null | undefined;
|
556 | /**
|
557 | Deserializes value of the query parameter based on defaultValueType
|
558 |
|
559 | @method deserializeQueryParam
|
560 | @param {Object} value
|
561 | @param {String} urlKey
|
562 | @param {String} defaultValueType
|
563 | @private
|
564 | */
|
565 | deserializeQueryParam(
|
566 | value: unknown,
|
567 | _urlKey: string,
|
568 | defaultValueType: string
|
569 | ): {} | null | undefined;
|
570 | /**
|
571 | @private
|
572 |
|
573 | @property _optionsForQueryParam
|
574 | */
|
575 | _optionsForQueryParam(qp: QueryParam): {};
|
576 | /**
|
577 | A hook you can use to reset controller values either when the model
|
578 | changes or the route is exiting.
|
579 |
|
580 | ```app/routes/articles.js
|
581 | import Route from '@ember/routing/route';
|
582 |
|
583 | export default class ArticlesRoute extends Route {
|
584 | resetController(controller, isExiting, transition) {
|
585 | if (isExiting && transition.targetName !== 'error') {
|
586 | controller.set('page', 1);
|
587 | }
|
588 | }
|
589 | }
|
590 | ```
|
591 |
|
592 | @method resetController
|
593 | @param {Controller} controller instance
|
594 | @param {Boolean} isExiting
|
595 | @param {Object} transition
|
596 | @since 1.7.0
|
597 | @public
|
598 | */
|
599 | resetController(_controller: Controller, _isExiting: boolean, _transition: Transition): void;
|
600 | /**
|
601 | @private
|
602 |
|
603 | @method exit
|
604 | */
|
605 | exit(transition?: Transition): void;
|
606 | /**
|
607 | @private
|
608 |
|
609 | @method _internalReset
|
610 | @since 3.6.0
|
611 | */
|
612 | _internalReset(isExiting: boolean, transition: Transition): void;
|
613 | /**
|
614 | @private
|
615 |
|
616 | @method enter
|
617 | */
|
618 | enter(transition: Transition): void;
|
619 | /**
|
620 | This event is triggered when the router enters the route. It is
|
621 | not executed when the model for the route changes.
|
622 |
|
623 | ```app/routes/application.js
|
624 | import { on } from '@ember/object/evented';
|
625 | import Route from '@ember/routing/route';
|
626 |
|
627 | export default Route.extend({
|
628 | collectAnalytics: on('activate', function(){
|
629 | collectAnalytics();
|
630 | })
|
631 | });
|
632 | ```
|
633 |
|
634 | @event activate
|
635 | @since 1.9.0
|
636 | @public
|
637 | */
|
638 | /**
|
639 | This event is triggered when the router completely exits this
|
640 | route. It is not executed when the model for the route changes.
|
641 |
|
642 | ```app/routes/index.js
|
643 | import { on } from '@ember/object/evented';
|
644 | import Route from '@ember/routing/route';
|
645 |
|
646 | export default Route.extend({
|
647 | trackPageLeaveAnalytics: on('deactivate', function(){
|
648 | trackPageLeaveAnalytics();
|
649 | })
|
650 | });
|
651 | ```
|
652 |
|
653 | @event deactivate
|
654 | @since 1.9.0
|
655 | @public
|
656 | */
|
657 | /**
|
658 | This hook is executed when the router completely exits this route. It is
|
659 | not executed when the model for the route changes.
|
660 |
|
661 | @method deactivate
|
662 | @param {Transition} transition
|
663 | @since 1.0.0
|
664 | @public
|
665 | */
|
666 | deactivate(_transition?: Transition): void;
|
667 | /**
|
668 | This hook is executed when the router enters the route. It is not executed
|
669 | when the model for the route changes.
|
670 |
|
671 | @method activate
|
672 | @param {Transition} transition
|
673 | @since 1.0.0
|
674 | @public
|
675 | */
|
676 | activate(_transition: Transition): void;
|
677 | /**
|
678 | Perform a synchronous transition into another route without attempting
|
679 | to resolve promises, update the URL, or abort any currently active
|
680 | asynchronous transitions (i.e. regular transitions caused by
|
681 | `transitionTo` or URL changes).
|
682 |
|
683 | This method is handy for performing intermediate transitions on the
|
684 | way to a final destination route, and is called internally by the
|
685 | default implementations of the `error` and `loading` handlers.
|
686 |
|
687 | @method intermediateTransitionTo
|
688 | @param {String} name the name of the route
|
689 | @param {...Object} models the model(s) to be used while transitioning
|
690 | to the route.
|
691 | @since 1.2.0
|
692 | @public
|
693 | */
|
694 | intermediateTransitionTo(...args: NamedRouteArgs): void;
|
695 | /**
|
696 | Refresh the model on this route and any child routes, firing the
|
697 | `beforeModel`, `model`, and `afterModel` hooks in a similar fashion
|
698 | to how routes are entered when transitioning in from other route.
|
699 | The current route params (e.g. `article_id`) will be passed in
|
700 | to the respective model hooks, and if a different model is returned,
|
701 | `setupController` and associated route hooks will re-fire as well.
|
702 |
|
703 | An example usage of this method is re-querying the server for the
|
704 | latest information using the same parameters as when the route
|
705 | was first entered.
|
706 |
|
707 | Note that this will cause `model` hooks to fire even on routes
|
708 | that were provided a model object when the route was initially
|
709 | entered.
|
710 |
|
711 | @method refresh
|
712 | @return {Transition} the transition object associated with this
|
713 | attempted transition
|
714 | @since 1.4.0
|
715 | @public
|
716 | */
|
717 | refresh(): Transition;
|
718 | /**
|
719 | This hook is the entry point for router.js
|
720 |
|
721 | @private
|
722 | @method setup
|
723 | */
|
724 | setup(context: Model | undefined, transition: Transition): void;
|
725 | _qpChanged(prop: string, value: unknown, qp: QueryParam): void;
|
726 | /**
|
727 | This hook is the first of the route entry validation hooks
|
728 | called when an attempt is made to transition into a route
|
729 | or one of its children. It is called before `model` and
|
730 | `afterModel`, and is appropriate for cases when:
|
731 |
|
732 | 1) A decision can be made to redirect elsewhere without
|
733 | needing to resolve the model first.
|
734 | 2) Any async operations need to occur first before the
|
735 | model is attempted to be resolved.
|
736 |
|
737 | This hook is provided the current `transition` attempt
|
738 | as a parameter, which can be used to `.abort()` the transition,
|
739 | save it for a later `.retry()`, or retrieve values set
|
740 | on it from a previous hook. You can also just call
|
741 | `router.transitionTo` to another route to implicitly
|
742 | abort the `transition`.
|
743 |
|
744 | You can return a promise from this hook to pause the
|
745 | transition until the promise resolves (or rejects). This could
|
746 | be useful, for instance, for retrieving async code from
|
747 | the server that is required to enter a route.
|
748 |
|
749 | @method beforeModel
|
750 | @param {Transition} transition
|
751 | @return {any | Promise<any>} if the value returned from this hook is
|
752 | a promise, the transition will pause until the transition
|
753 | resolves. Otherwise, non-promise return values are not
|
754 | utilized in any way.
|
755 | @since 1.0.0
|
756 | @public
|
757 | */
|
758 | beforeModel(_transition: Transition): unknown | Promise<unknown>;
|
759 | /**
|
760 | This hook is called after this route's model has resolved.
|
761 | It follows identical async/promise semantics to `beforeModel`
|
762 | but is provided the route's resolved model in addition to
|
763 | the `transition`, and is therefore suited to performing
|
764 | logic that can only take place after the model has already
|
765 | resolved.
|
766 |
|
767 | ```app/routes/posts.js
|
768 | import Route from '@ember/routing/route';
|
769 | import { service } from '@ember/service';
|
770 |
|
771 | export default class PostsRoute extends Route {
|
772 | @service router;
|
773 |
|
774 | afterModel(posts, transition) {
|
775 | if (posts.get('length') === 1) {
|
776 | this.router.transitionTo('post.show', posts.get('firstObject'));
|
777 | }
|
778 | }
|
779 | }
|
780 | ```
|
781 |
|
782 | Refer to documentation for `beforeModel` for a description
|
783 | of transition-pausing semantics when a promise is returned
|
784 | from this hook.
|
785 |
|
786 | @method afterModel
|
787 | @param {Object} resolvedModel the value returned from `model`,
|
788 | or its resolved value if it was a promise
|
789 | @param {Transition} transition
|
790 | @return {any | Promise<any>} if the value returned from this hook is
|
791 | a promise, the transition will pause until the transition
|
792 | resolves. Otherwise, non-promise return values are not
|
793 | utilized in any way.
|
794 | @since 1.0.0
|
795 | @public
|
796 | */
|
797 | afterModel(
|
798 | _resolvedModel: Model | undefined,
|
799 | _transition: Transition
|
800 | ): unknown | Promise<unknown>;
|
801 | /**
|
802 | A hook you can implement to optionally redirect to another route.
|
803 |
|
804 | Calling `this.router.transitionTo` from inside of the `redirect` hook will
|
805 | abort the current transition (into the route that has implemented `redirect`).
|
806 |
|
807 | `redirect` and `afterModel` behave very similarly and are
|
808 | called almost at the same time, but they have an important
|
809 | distinction when calling `this.router.transitionTo` to a child route
|
810 | of the current route. From `afterModel`, this new transition
|
811 | invalidates the current transition, causing `beforeModel`,
|
812 | `model`, and `afterModel` hooks to be called again. But the
|
813 | same transition started from `redirect` does _not_ invalidate
|
814 | the current transition. In other words, by the time the `redirect`
|
815 | hook has been called, both the resolved model and the attempted
|
816 | entry into this route are considered fully validated.
|
817 |
|
818 | @method redirect
|
819 | @param {Object} model the model for this route
|
820 | @param {Transition} transition the transition object associated with the current transition
|
821 | @since 1.0.0
|
822 | @public
|
823 | */
|
824 | redirect(_model: Model, _transition: Transition): void;
|
825 | /**
|
826 | Called when the context is changed by router.js.
|
827 |
|
828 | @private
|
829 | @method contextDidChange
|
830 | */
|
831 | contextDidChange(): void;
|
832 | /**
|
833 | A hook you can implement to convert the URL into the model for
|
834 | this route.
|
835 |
|
836 | ```app/router.js
|
837 |
|
838 |
|
839 | Router.map(function() {
|
840 | this.route('post', { path: '/posts/:post_id' });
|
841 | });
|
842 |
|
843 | export default Router;
|
844 | ```
|
845 |
|
846 | Note that for routes with dynamic segments, this hook is not always
|
847 | executed. If the route is entered through a transition (e.g. when
|
848 | using the `link-to` Handlebars helper or the `transitionTo` method
|
849 | of routes), and a model context is already provided this hook
|
850 | is not called.
|
851 |
|
852 | A model context does not include a primitive string or number,
|
853 | which does cause the model hook to be called.
|
854 |
|
855 | Routes without dynamic segments will always execute the model hook.
|
856 |
|
857 | ```javascript
|
858 |
|
859 | this.router.transitionTo('posts');
|
860 |
|
861 |
|
862 | thePost = store.findRecord('post', 1);
|
863 | this.router.transitionTo('post', thePost);
|
864 |
|
865 |
|
866 | this.router.transitionTo('post', 1);
|
867 |
|
868 |
|
869 |
|
870 | thePost = store.findRecord('post', 1);
|
871 | this.router.transitionTo('post', thePost.id);
|
872 | ```
|
873 |
|
874 | This hook follows the asynchronous/promise semantics
|
875 | described in the documentation for `beforeModel`. In particular,
|
876 | if a promise returned from `model` fails, the error will be
|
877 | handled by the `error` hook on `Route`.
|
878 |
|
879 | Note that the legacy behavior of automatically defining a model
|
880 | hook when a dynamic segment ending in `_id` is present is
|
881 | [deprecated](https://deprecations.emberjs.com/v5.x#toc_deprecate-implicit-route-model).
|
882 | You should explicitly define a model hook whenever any segments are
|
883 | present.
|
884 |
|
885 | Example
|
886 |
|
887 | ```app/routes/post.js
|
888 | import Route from '@ember/routing/route';
|
889 | import { service } from '@ember/service';
|
890 |
|
891 | export default class PostRoute extends Route {
|
892 | @service store;
|
893 |
|
894 | model(params) {
|
895 | return this.store.findRecord('post', params.post_id);
|
896 | }
|
897 | }
|
898 | ```
|
899 |
|
900 | @method model
|
901 | @param {Object} params the parameters extracted from the URL
|
902 | @param {Transition} transition
|
903 | @return {any | Promise<any>} the model for this route. If
|
904 | a promise is returned, the transition will pause until
|
905 | the promise resolves, and the resolved value of the promise
|
906 | will be used as the model for this route.
|
907 | @since 1.0.0
|
908 | @public
|
909 | */
|
910 | model(
|
911 | params: Record<string, unknown>,
|
912 | transition: Transition
|
913 | ): Model | PromiseLike<Model> | undefined;
|
914 | /**
|
915 | @private
|
916 | @method deserialize
|
917 | @param {Object} params the parameters extracted from the URL
|
918 | @param {Transition} transition
|
919 | @return {any | Promise<any>} the model for this route.
|
920 |
|
921 | Router.js hook.
|
922 | */
|
923 | deserialize(
|
924 | _params: Record<string, unknown>,
|
925 | transition: Transition
|
926 | ): Model | PromiseLike<Model> | undefined;
|
927 | /**
|
928 |
|
929 | @method findModel
|
930 | @param {String} type the model type
|
931 | @param {Object} value the value passed to find
|
932 | @private
|
933 | */
|
934 | findModel(type: string, value: unknown): Model | PromiseLike<Model> | undefined;
|
935 | /**
|
936 | A hook you can use to setup the controller for the current route.
|
937 |
|
938 | This method is called with the controller for the current route and the
|
939 | model supplied by the `model` hook.
|
940 |
|
941 | By default, the `setupController` hook sets the `model` property of
|
942 | the controller to the specified `model` when it is not `undefined`.
|
943 |
|
944 | If you implement the `setupController` hook in your Route, it will
|
945 | prevent this default behavior. If you want to preserve that behavior
|
946 | when implementing your `setupController` function, make sure to call
|
947 | `super`:
|
948 |
|
949 | ```app/routes/photos.js
|
950 | import Route from '@ember/routing/route';
|
951 | import { service } from '@ember/service';
|
952 |
|
953 | export default class PhotosRoute extends Route {
|
954 | @service store;
|
955 |
|
956 | model() {
|
957 | return this.store.findAll('photo');
|
958 | }
|
959 |
|
960 | setupController(controller, model) {
|
961 | super.setupController(controller, model);
|
962 |
|
963 | this.controllerFor('application').set('showingPhotos', true);
|
964 | }
|
965 | }
|
966 | ```
|
967 |
|
968 | The provided controller will be one resolved based on the name
|
969 | of this route.
|
970 |
|
971 | If no explicit controller is defined, Ember will automatically create one.
|
972 |
|
973 | As an example, consider the router:
|
974 |
|
975 | ```app/router.js
|
976 |
|
977 |
|
978 | Router.map(function() {
|
979 | this.route('post', { path: '/posts/:post_id' });
|
980 | });
|
981 |
|
982 | export default Router;
|
983 | ```
|
984 |
|
985 | If you have defined a file for the post controller,
|
986 | the framework will use it.
|
987 | If it is not defined, a basic `Controller` instance would be used.
|
988 |
|
989 | @example Behavior of a basic Controller
|
990 |
|
991 | ```app/routes/post.js
|
992 | import Route from '@ember/routing/route';
|
993 |
|
994 | export default class PostRoute extends Route {
|
995 | setupController(controller, model) {
|
996 | controller.set('model', model);
|
997 | }
|
998 | });
|
999 | ```
|
1000 |
|
1001 | @method setupController
|
1002 | @param {Controller} controller instance
|
1003 | @param {Object} model
|
1004 | @param {Transition} [transition]
|
1005 | @since 1.0.0
|
1006 | @public
|
1007 | */
|
1008 | setupController(
|
1009 | controller: Controller,
|
1010 | context: Model | undefined,
|
1011 | _transition?: Transition
|
1012 | ): void;
|
1013 | /**
|
1014 | Returns the controller of the current route, or a parent (or any ancestor)
|
1015 | route in a route hierarchy.
|
1016 |
|
1017 | The controller instance must already have been created, either through entering the
|
1018 | associated route or using `generateController`.
|
1019 |
|
1020 | ```app/routes/post.js
|
1021 | import Route from '@ember/routing/route';
|
1022 |
|
1023 | export default class PostRoute extends Route {
|
1024 | setupController(controller, post) {
|
1025 | super.setupController(controller, post);
|
1026 |
|
1027 | this.controllerFor('posts').set('currentPost', post);
|
1028 | }
|
1029 | }
|
1030 | ```
|
1031 |
|
1032 | @method controllerFor
|
1033 | @param {String} name the name of the route or controller
|
1034 | @return {Controller | undefined}
|
1035 | @since 1.0.0
|
1036 | @public
|
1037 | */
|
1038 | controllerFor(name: string, _skipAssert: true): Controller | undefined;
|
1039 | controllerFor(name: string, _skipAssert?: false): Controller;
|
1040 | /**
|
1041 | Generates a controller for a route.
|
1042 |
|
1043 | Example
|
1044 |
|
1045 | ```app/routes/post.js
|
1046 | import Route from '@ember/routing/route';
|
1047 |
|
1048 | export default class Post extends Route {
|
1049 | setupController(controller, post) {
|
1050 | super.setupController(controller, post);
|
1051 |
|
1052 | this.generateController('posts');
|
1053 | }
|
1054 | }
|
1055 | ```
|
1056 |
|
1057 | @method generateController
|
1058 | @param {String} name the name of the controller
|
1059 | @private
|
1060 | */
|
1061 | generateController(name: string): Controller<unknown>;
|
1062 | /**
|
1063 | Returns the resolved model of a parent (or any ancestor) route
|
1064 | in a route hierarchy. During a transition, all routes
|
1065 | must resolve a model object, and if a route
|
1066 | needs access to a parent route's model in order to
|
1067 | resolve a model (or just reuse the model from a parent),
|
1068 | it can call `this.modelFor(theNameOfParentRoute)` to
|
1069 | retrieve it. If the ancestor route's model was a promise,
|
1070 | its resolved result is returned.
|
1071 |
|
1072 | Example
|
1073 |
|
1074 | ```app/router.js
|
1075 |
|
1076 |
|
1077 | Router.map(function() {
|
1078 | this.route('post', { path: '/posts/:post_id' }, function() {
|
1079 | this.route('comments');
|
1080 | });
|
1081 | });
|
1082 |
|
1083 | export default Router;
|
1084 | ```
|
1085 |
|
1086 | ```app/routes/post/comments.js
|
1087 | import Route from '@ember/routing/route';
|
1088 |
|
1089 | export default class PostCommentsRoute extends Route {
|
1090 | model() {
|
1091 | let post = this.modelFor('post');
|
1092 |
|
1093 | return post.comments;
|
1094 | }
|
1095 | }
|
1096 | ```
|
1097 |
|
1098 | @method modelFor
|
1099 | @param {String} name the name of the route
|
1100 | @return {Object} the model object
|
1101 | @since 1.0.0
|
1102 | @public
|
1103 | */
|
1104 | modelFor(_name: string): unknown | undefined;
|
1105 | [RENDER_STATE]: RenderState | undefined;
|
1106 | /**
|
1107 | `this[RENDER]` is used to set up the rendering option for the outlet state.
|
1108 | @method this[RENDER]
|
1109 | @private
|
1110 | */
|
1111 | [RENDER](): void;
|
1112 | willDestroy(): void;
|
1113 | /**
|
1114 | @private
|
1115 |
|
1116 | @method teardownViews
|
1117 | */
|
1118 | teardownViews(): void;
|
1119 | /**
|
1120 | Allows you to produce custom metadata for the route.
|
1121 | The return value of this method will be attached to
|
1122 | its corresponding RouteInfoWithAttributes object.
|
1123 |
|
1124 | Example
|
1125 |
|
1126 | ```app/routes/posts/index.js
|
1127 | import Route from '@ember/routing/route';
|
1128 |
|
1129 | export default class PostsIndexRoute extends Route {
|
1130 | buildRouteInfoMetadata() {
|
1131 | return { title: 'Posts Page' }
|
1132 | }
|
1133 | }
|
1134 | ```
|
1135 |
|
1136 | ```app/routes/application.js
|
1137 | import Route from '@ember/routing/route';
|
1138 | import { service } from '@ember/service';
|
1139 |
|
1140 | export default class ApplicationRoute extends Route {
|
1141 | @service router
|
1142 |
|
1143 | constructor() {
|
1144 | super(...arguments);
|
1145 |
|
1146 | this.router.on('routeDidChange', transition => {
|
1147 | document.title = transition.to.metadata.title;
|
1148 |
|
1149 | });
|
1150 | }
|
1151 | }
|
1152 | ```
|
1153 | @method buildRouteInfoMetadata
|
1154 | @return any
|
1155 | @since 3.10.0
|
1156 | @public
|
1157 | */
|
1158 | buildRouteInfoMetadata(): unknown;
|
1159 | private _paramsFor;
|
1160 | /** @deprecated Manually define your own store, such as with `@service store` */
|
1161 | protected get _store(): {
|
1162 | find(name: string, value: unknown): any;
|
1163 | };
|
1164 | /**
|
1165 | @private
|
1166 | @property _qp
|
1167 | */
|
1168 | protected get _qp(): QueryParamMeta;
|
1169 | actions: Record<string, AnyFn>;
|
1170 | /**
|
1171 | Sends an action to the router, which will delegate it to the currently
|
1172 | active route hierarchy per the bubbling rules explained under `actions`.
|
1173 |
|
1174 | Example
|
1175 |
|
1176 | ```app/router.js
|
1177 |
|
1178 |
|
1179 | Router.map(function() {
|
1180 | this.route('index');
|
1181 | });
|
1182 |
|
1183 | export default Router;
|
1184 | ```
|
1185 |
|
1186 | ```app/routes/application.js
|
1187 | import Route from '@ember/routing/route';
|
1188 | import { action } from '@ember/object';
|
1189 |
|
1190 | export default class ApplicationRoute extends Route {
|
1191 | @action
|
1192 | track(arg) {
|
1193 | console.log(arg, 'was clicked');
|
1194 | }
|
1195 | }
|
1196 | ```
|
1197 |
|
1198 | ```app/routes/index.js
|
1199 | import Route from '@ember/routing/route';
|
1200 | import { action } from '@ember/object';
|
1201 |
|
1202 | export default class IndexRoute extends Route {
|
1203 | @action
|
1204 | trackIfDebug(arg) {
|
1205 | if (debug) {
|
1206 | this.send('track', arg);
|
1207 | }
|
1208 | }
|
1209 | }
|
1210 | ```
|
1211 |
|
1212 | @method send
|
1213 | @param {String} name the name of the action to trigger
|
1214 | @param {...*} args
|
1215 | @since 1.0.0
|
1216 | @public
|
1217 | */
|
1218 | send: <K extends keyof this | keyof this['actions']>(
|
1219 | name: K,
|
1220 | ...args: MaybeParameters<
|
1221 | K extends keyof this
|
1222 | ? this[K]
|
1223 | : K extends keyof this['actions']
|
1224 | ? this['actions'][K]
|
1225 | : never
|
1226 | >
|
1227 | ) => MaybeReturnType<
|
1228 | K extends keyof this ? this[K] : K extends keyof this['actions'] ? this['actions'][K] : never
|
1229 | >;
|
1230 | }
|
1231 | export function getRenderState(route: Route): RenderState | undefined;
|
1232 | export function getFullQueryParams(
|
1233 | router: EmberRouter,
|
1234 | state: RouteTransitionState
|
1235 | ): Record<string, unknown>;
|
1236 | const defaultSerialize: (
|
1237 | model: any,
|
1238 | params: string[]
|
1239 | ) =>
|
1240 | | {
|
1241 | [key: string]: unknown;
|
1242 | }
|
1243 | | undefined;
|
1244 | export { defaultSerialize };
|
1245 | export function hasDefaultSerialize(route: Route): boolean;
|
1246 | export default Route;
|
1247 | }
|
1248 |
|
\ | No newline at end of file |