UNPKG

3.35 kBTypeScriptView Raw
1declare module '@ember/service' {
2 import { FrameworkObject } from '@ember/object/-internals';
3 import type { DecoratorPropertyDescriptor, ElementDescriptor } from '@ember/-internals/metal';
4 /**
5 @module @ember/service
6 @public
7 */
8 /**
9 @method inject
10 @static
11 @since 1.10.0
12 @for @ember/service
13 @param {String} name (optional) name of the service to inject, defaults to
14 the property's name
15 @return {ComputedDecorator} injection decorator instance
16 @public
17 */
18 export function inject(name: string): PropertyDecorator;
19 export function inject(...args: [ElementDescriptor[0], ElementDescriptor[1]]): void;
20 export function inject(...args: ElementDescriptor): DecoratorPropertyDescriptor;
21 export function inject(): PropertyDecorator;
22 /**
23 Creates a property that lazily looks up a service in the container. There are
24 no restrictions as to what objects a service can be injected into.
25
26 Example:
27
28 ```app/routes/application.js
29 import Route from '@ember/routing/route';
30 import { service } from '@ember/service';
31
32 export default class ApplicationRoute extends Route {
33 @service('auth') authManager;
34
35 model() {
36 return this.authManager.findCurrentUser();
37 }
38 }
39 ```
40
41 Classic Class Example:
42
43 ```app/routes/application.js
44 import Route from '@ember/routing/route';
45 import { service } from '@ember/service';
46
47 export default Route.extend({
48 authManager: service('auth'),
49
50 model() {
51 return this.get('authManager').findCurrentUser();
52 }
53 });
54 ```
55
56 This example will create an `authManager` property on the application route
57 that looks up the `auth` service in the container, making it easily accessible
58 in the `model` hook.
59
60 @method service
61 @static
62 @since 4.1.0
63 @for @ember/service
64 @param {String} name (optional) name of the service to inject, defaults to
65 the property's name
66 @return {ComputedDecorator} injection decorator instance
67 @public
68 */
69 export function service(name: string): PropertyDecorator;
70 export function service(...args: [ElementDescriptor[0], ElementDescriptor[1]]): void;
71 export function service(...args: ElementDescriptor): DecoratorPropertyDescriptor;
72 export function service(): PropertyDecorator;
73 /**
74 @class Service
75 @extends EmberObject
76 @since 1.10.0
77 @public
78 */
79 export default class Service extends FrameworkObject {
80 static isServiceFactory: boolean;
81 }
82 /**
83 A type registry for Ember `Service`s. Meant to be declaration-merged so string
84 lookups resolve to the correct type.
85
86 Blueprints should include such a declaration merge for TypeScript:
87
88 ```ts
89 import Service from '@ember/service';
90
91 export default class ExampleService extends Service {
92 // ...
93 }
94
95 declare module '@ember/service' {
96 export interface Registry {
97 example: ExampleService;
98 }
99 }
100 ```
101
102 Then `@service` can check that the service is registered correctly, and APIs
103 like `owner.lookup('service:example')` can return `ExampleService`.
104 */
105 export interface Registry extends Record<string, object | undefined> {}
106}