UNPKG

4.38 kBTypeScriptView Raw
1declare module '@ember/object/mixin' {
2 /**
3 @module @ember/object/mixin
4 */
5 import { INIT_FACTORY } from '@ember/-internals/container';
6 export function applyMixin(
7 obj: Record<string, any>,
8 mixins: Array<Mixin | Record<string, unknown>>,
9 _hideKeys?: boolean
10 ): Record<string, any>;
11 /**
12 @method mixin
13 @param obj
14 @param mixins*
15 @return obj
16 @private
17 */
18 export function mixin(obj: object, ...args: any[]): object;
19 /**
20 The `Mixin` class allows you to create mixins, whose properties can be
21 added to other classes. For instance,
22
23 ```javascript
24 import Mixin from '@ember/object/mixin';
25
26 const EditableMixin = Mixin.create({
27 edit() {
28 console.log('starting to edit');
29 this.set('isEditing', true);
30 },
31 isEditing: false
32 });
33 ```
34
35 ```javascript
36 import EmberObject from '@ember/object';
37 import EditableMixin from '../mixins/editable';
38
39 // Mix mixins into classes by passing them as the first arguments to
40 // `.extend.`
41 const Comment = EmberObject.extend(EditableMixin, {
42 post: null
43 });
44
45 let comment = Comment.create({
46 post: somePost
47 });
48
49 comment.edit(); // outputs 'starting to edit'
50 ```
51
52 Note that Mixins are created with `Mixin.create`, not
53 `Mixin.extend`.
54
55 Note that mixins extend a constructor's prototype so arrays and object literals
56 defined as properties will be shared amongst objects that implement the mixin.
57 If you want to define a property in a mixin that is not shared, you can define
58 it either as a computed property or have it be created on initialization of the object.
59
60 ```javascript
61 // filters array will be shared amongst any object implementing mixin
62 import Mixin from '@ember/object/mixin';
63 import { A } from '@ember/array';
64
65 const FilterableMixin = Mixin.create({
66 filters: A()
67 });
68 ```
69
70 ```javascript
71 import Mixin from '@ember/object/mixin';
72 import { A } from '@ember/array';
73 import { computed } from '@ember/object';
74
75 // filters will be a separate array for every object implementing the mixin
76 const FilterableMixin = Mixin.create({
77 filters: computed(function() {
78 return A();
79 })
80 });
81 ```
82
83 ```javascript
84 import Mixin from '@ember/object/mixin';
85 import { A } from '@ember/array';
86
87 // filters will be created as a separate array during the object's initialization
88 const Filterable = Mixin.create({
89 filters: null,
90
91 init() {
92 this._super(...arguments);
93 this.set("filters", A());
94 }
95 });
96 ```
97
98 @class Mixin
99 @public
100 */
101 export default class Mixin {
102 /** @internal */
103 static _disableDebugSeal?: boolean;
104 /** @internal */
105 mixins: Mixin[] | undefined;
106 /** @internal */
107 properties:
108 | {
109 [key: string]: any;
110 }
111 | undefined;
112 /** @internal */
113 ownerConstructor: any;
114 /** @internal */
115 _without: any[] | undefined;
116 [INIT_FACTORY]?: null;
117 /** @internal */
118 constructor(
119 mixins: Mixin[] | undefined,
120 properties?: {
121 [key: string]: any;
122 }
123 );
124 /**
125 @method create
126 @for @ember/object/mixin
127 @static
128 @param arguments*
129 @public
130 */
131 static create<M extends typeof Mixin>(...args: any[]): InstanceType<M>;
132 /** @internal */
133 static mixins(obj: object): Mixin[];
134 /**
135 @method reopen
136 @param arguments*
137 @private
138 @internal
139 */
140 reopen(...args: Array<Mixin | Record<string, unknown>>): this;
141 /**
142 @method apply
143 @param obj
144 @return applied object
145 @private
146 @internal
147 */
148 apply(obj: object, _hideKeys?: boolean): Record<string, any>;
149 /** @internal */
150 applyPartial(obj: object): Record<string, any>;
151 /**
152 @method detect
153 @param obj
154 @return {Boolean}
155 @private
156 @internal
157 */
158 detect(obj: any): boolean;
159 /** @internal */
160 without(...args: any[]): Mixin;
161 /** @internal */
162 keys(): Set<string>;
163 /** @internal */
164 toString(): string;
165 }
166}