UNPKG

11.3 kBTypeScriptView Raw
1declare module '@ember/controller' {
2 import { FrameworkObject } from '@ember/object/-internals';
3 import type { DecoratorPropertyDescriptor, ElementDescriptor } from '@ember/-internals/metal';
4 import Mixin from '@ember/object/mixin';
5 import type { RouteArgs } from '@ember/routing/-internals';
6 import { ActionHandler } from '@ember/-internals/runtime';
7 import type { Transition } from 'router_js';
8 export type ControllerQueryParamType = 'boolean' | 'number' | 'array' | 'string';
9 export type ControllerQueryParam =
10 | string
11 | Record<
12 string,
13 {
14 type: ControllerQueryParamType;
15 }
16 >
17 | Record<string, string>;
18 /**
19 @module @ember/controller
20 */
21 /**
22 @class ControllerMixin
23 @namespace Ember
24 @uses Ember.ActionHandler
25 @private
26 */
27 interface ControllerMixin<T> extends ActionHandler {
28 /** @internal */
29 _qpDelegate: unknown | null;
30 isController: true;
31 /**
32 The object to which actions from the view should be sent.
33
34 For example, when a Handlebars template uses the `{{action}}` helper,
35 it will attempt to send the action to the view's controller's `target`.
36
37 By default, the value of the target property is set to the router, and
38 is injected when a controller is instantiated. This injection is applied
39 as part of the application's initialization process. In most cases the
40 `target` property will automatically be set to the logical consumer of
41 actions for the controller.
42
43 @property target
44 @default null
45 @public
46 */
47 target: unknown | null;
48 /**
49 The controller's current model. When retrieving or modifying a controller's
50 model, this property should be used instead of the `content` property.
51
52 @property model
53 @public
54 */
55 model: T;
56 /**
57 Defines which query parameters the controller accepts.
58 If you give the names `['category','page']` it will bind
59 the values of these query parameters to the variables
60 `this.category` and `this.page`.
61
62 By default, query parameters are parsed as strings. This
63 may cause unexpected behavior if a query parameter is used with `toggleProperty`,
64 because the initial value set for `param=false` will be the string `"false"`, which is truthy.
65
66 To avoid this, you may specify that the query parameter should be parsed as a boolean
67 by using the following verbose form with a `type` property:
68 ```javascript
69 queryParams: [{
70 category: {
71 type: 'boolean'
72 }
73 }]
74 ```
75 Available values for the `type` parameter are `'boolean'`, `'number'`, `'array'`, and `'string'`.
76 If query param type is not specified, it will default to `'string'`.
77
78 @for Ember.ControllerMixin
79 @property queryParams
80 @public
81 */
82 queryParams: Readonly<Array<ControllerQueryParam>>;
83 /**
84 Transition the application into another route. The route may
85 be either a single route or route path:
86
87 ```javascript
88 aController.transitionToRoute('blogPosts');
89 aController.transitionToRoute('blogPosts.recentEntries');
90 ```
91
92 Optionally supply a model for the route in question. The model
93 will be serialized into the URL using the `serialize` hook of
94 the route:
95
96 ```javascript
97 aController.transitionToRoute('blogPost', aPost);
98 ```
99
100 If a literal is passed (such as a number or a string), it will
101 be treated as an identifier instead. In this case, the `model`
102 hook of the route will be triggered:
103
104 ```javascript
105 aController.transitionToRoute('blogPost', 1);
106 ```
107
108 Multiple models will be applied last to first recursively up the
109 route tree.
110
111 ```app/router.js
112 Router.map(function() {
113 this.route('blogPost', { path: ':blogPostId' }, function() {
114 this.route('blogComment', { path: ':blogCommentId', resetNamespace: true });
115 });
116 });
117 ```
118
119 ```javascript
120 aController.transitionToRoute('blogComment', aPost, aComment);
121 aController.transitionToRoute('blogComment', 1, 13);
122 ```
123
124 It is also possible to pass a URL (a string that starts with a
125 `/`).
126
127 ```javascript
128 aController.transitionToRoute('/');
129 aController.transitionToRoute('/blog/post/1/comment/13');
130 aController.transitionToRoute('/blog/posts?sort=title');
131 ```
132
133 An options hash with a `queryParams` property may be provided as
134 the final argument to add query parameters to the destination URL.
135
136 ```javascript
137 aController.transitionToRoute('blogPost', 1, {
138 queryParams: { showComments: 'true' }
139 });
140
141 // if you just want to transition the query parameters without changing the route
142 aController.transitionToRoute({ queryParams: { sort: 'date' } });
143 ```
144
145 See also [replaceRoute](/ember/release/classes/Ember.ControllerMixin/methods/replaceRoute?anchor=replaceRoute).
146
147 @for Ember.ControllerMixin
148 @method transitionToRoute
149 @deprecated Use transitionTo from the Router service instead.
150 @param {String} [name] the name of the route or a URL
151 @param {...Object} models the model(s) or identifier(s) to be used
152 while transitioning to the route.
153 @param {Object} [options] optional hash with a queryParams property
154 containing a mapping of query parameters
155 @return {Transition} the transition object associated with this
156 attempted transition
157 @public
158 */
159 transitionToRoute(...args: RouteArgs): Transition;
160 /**
161 Transition into another route while replacing the current URL, if possible.
162 This will replace the current history entry instead of adding a new one.
163 Beside that, it is identical to `transitionToRoute` in all other respects.
164
165 ```javascript
166 aController.replaceRoute('blogPosts');
167 aController.replaceRoute('blogPosts.recentEntries');
168 ```
169
170 Optionally supply a model for the route in question. The model
171 will be serialized into the URL using the `serialize` hook of
172 the route:
173
174 ```javascript
175 aController.replaceRoute('blogPost', aPost);
176 ```
177
178 If a literal is passed (such as a number or a string), it will
179 be treated as an identifier instead. In this case, the `model`
180 hook of the route will be triggered:
181
182 ```javascript
183 aController.replaceRoute('blogPost', 1);
184 ```
185
186 Multiple models will be applied last to first recursively up the
187 route tree.
188
189 ```app/router.js
190 Router.map(function() {
191 this.route('blogPost', { path: ':blogPostId' }, function() {
192 this.route('blogComment', { path: ':blogCommentId', resetNamespace: true });
193 });
194 });
195 ```
196
197 ```
198 aController.replaceRoute('blogComment', aPost, aComment);
199 aController.replaceRoute('blogComment', 1, 13);
200 ```
201
202 It is also possible to pass a URL (a string that starts with a
203 `/`).
204
205 ```javascript
206 aController.replaceRoute('/');
207 aController.replaceRoute('/blog/post/1/comment/13');
208 ```
209
210 @for Ember.ControllerMixin
211 @method replaceRoute
212 @deprecated Use replaceWith from the Router service instead.
213 @param {String} [name] the name of the route or a URL
214 @param {...Object} models the model(s) or identifier(s) to be used
215 while transitioning to the route.
216 @param {Object} [options] optional hash with a queryParams property
217 containing a mapping of query parameters
218 @return {Transition} the transition object associated with this
219 attempted transition
220 @public
221 */
222 replaceRoute(...args: RouteArgs): Transition;
223 }
224 const ControllerMixin: Mixin;
225 /**
226 @class Controller
227 @extends EmberObject
228 @uses Ember.ControllerMixin
229 @public
230 */
231 interface Controller<_T = unknown> extends FrameworkObject, ControllerMixin<_T> {}
232 const Controller_base: Readonly<typeof import('@ember/object').default> &
233 (new (
234 owner?: import('@ember/-internals/owner').default | undefined
235 ) => import('@ember/object').default) &
236 Mixin;
237 class Controller<_T = unknown> extends Controller_base {}
238 /**
239 Creates a property that lazily looks up another controller in the container.
240 Can only be used when defining another controller.
241
242 Example:
243
244 ```app/controllers/post.js
245 import Controller, {
246 inject as controller
247 } from '@ember/controller';
248
249 export default class PostController extends Controller {
250 @controller posts;
251 }
252 ```
253
254 Classic Class Example:
255
256 ```app/controllers/post.js
257 import Controller, {
258 inject as controller
259 } from '@ember/controller';
260
261 export default Controller.extend({
262 posts: controller()
263 });
264 ```
265
266 This example will create a `posts` property on the `post` controller that
267 looks up the `posts` controller in the container, making it easy to reference
268 other controllers.
269
270 @method inject
271 @static
272 @for @ember/controller
273 @since 1.10.0
274 @param {String} name (optional) name of the controller to inject, defaults to
275 the property's name
276 @return {ComputedDecorator} injection decorator instance
277 @public
278 */
279 export function inject(name: string): PropertyDecorator;
280 export function inject(...args: [ElementDescriptor[0], ElementDescriptor[1]]): void;
281 export function inject(...args: ElementDescriptor): DecoratorPropertyDescriptor;
282 export function inject(): PropertyDecorator;
283 export { Controller as default, ControllerMixin };
284 /**
285 A type registry for Ember `Controller`s. Meant to be declaration-merged so string
286 lookups resolve to the correct type.
287
288 Blueprints should include such a declaration merge for TypeScript:
289
290 ```ts
291 import Controller from '@ember/controller';
292
293 export default class ExampleController extends Controller {
294 // ...
295 }
296
297 declare module '@ember/controller' {
298 export interface Registry {
299 example: ExampleController;
300 }
301 }
302 ```
303
304 Then `@inject` can check that the service is registered correctly, and APIs
305 like `owner.lookup('controller:example')` can return `ExampleController`.
306 */
307 export interface Registry extends Record<string, Controller | undefined> {}
308}