UNPKG

3.81 kBTypeScriptView Raw
1declare module '@ember/object/proxy' {
2 /**
3 @module @ember/object/proxy
4 */
5 import { FrameworkObject } from '@ember/object/-internals';
6 import { _ProxyMixin } from '@ember/-internals/runtime';
7 /**
8 `ObjectProxy` forwards all properties not defined by the proxy itself
9 to a proxied `content` object.
10
11 ```javascript
12 import EmberObject from '@ember/object';
13 import ObjectProxy from '@ember/object/proxy';
14
15 let exampleObject = EmberObject.create({
16 name: 'Foo'
17 });
18
19 let exampleProxy = ObjectProxy.create({
20 content: exampleObject
21 });
22
23 // Access and change existing properties
24 exampleProxy.get('name'); // 'Foo'
25 exampleProxy.set('name', 'Bar');
26 exampleObject.get('name'); // 'Bar'
27
28 // Create new 'description' property on `exampleObject`
29 exampleProxy.set('description', 'Foo is a whizboo baz');
30 exampleObject.get('description'); // 'Foo is a whizboo baz'
31 ```
32
33 While `content` is unset, setting a property to be delegated will throw an
34 Error.
35
36 ```javascript
37 import ObjectProxy from '@ember/object/proxy';
38
39 let exampleProxy = ObjectProxy.create({
40 content: null,
41 flag: null
42 });
43 exampleProxy.set('flag', true);
44 exampleProxy.get('flag'); // true
45 exampleProxy.get('foo'); // undefined
46 exampleProxy.set('foo', 'data'); // throws Error
47 ```
48
49 Delegated properties can be bound to and will change when content is updated.
50
51 Computed properties on the proxy itself can depend on delegated properties.
52
53 ```javascript
54 import { computed } from '@ember/object';
55 import ObjectProxy from '@ember/object/proxy';
56
57 ProxyWithComputedProperty = ObjectProxy.extend({
58 fullName: computed('firstName', 'lastName', function() {
59 var firstName = this.get('firstName'),
60 lastName = this.get('lastName');
61 if (firstName && lastName) {
62 return firstName + ' ' + lastName;
63 }
64 return firstName || lastName;
65 })
66 });
67
68 let exampleProxy = ProxyWithComputedProperty.create();
69
70 exampleProxy.get('fullName'); // undefined
71 exampleProxy.set('content', {
72 firstName: 'Tom', lastName: 'Dale'
73 }); // triggers property change for fullName on proxy
74
75 exampleProxy.get('fullName'); // 'Tom Dale'
76 ```
77
78 @class ObjectProxy
79 @extends EmberObject
80 @uses Ember.ProxyMixin
81 @public
82 */
83 interface ObjectProxy<Content = unknown> extends _ProxyMixin<Content> {
84 get<K extends keyof Content>(keyName: K): Content[K];
85 get<K extends keyof this>(keyname: K): this[K];
86 get(keyName: string): unknown;
87 set<K extends keyof Content>(keyName: K, value: Content[K]): Content[K];
88 set<K extends keyof this>(keyName: K, value: this[K]): this[K];
89 set(keyName: string): unknown;
90 getProperties<K extends keyof Content | keyof this>(
91 list: K[]
92 ): Pick<Content, Exclude<K, keyof this>> & Pick<this, Exclude<K, keyof Content>>;
93 getProperties<K extends keyof Content | keyof this>(
94 ...list: K[]
95 ): Pick<Content, Exclude<K, keyof this>> & Pick<this, Exclude<K, keyof Content>>;
96 getProperties<K extends string>(list: K[]): Record<K, unknown>;
97 getProperties<K extends string>(...list: K[]): Record<K, unknown>;
98 setProperties<
99 K extends keyof Content | keyof this,
100 Hash extends Partial<
101 Pick<Content, Exclude<K, keyof this>> & Pick<this, Exclude<K, keyof Content>>
102 >
103 >(
104 hash: Hash
105 ): Hash;
106 setProperties<T extends Record<string, unknown>>(hash: T): T;
107 }
108 class ObjectProxy<Content = unknown> extends FrameworkObject {}
109 export default ObjectProxy;
110}