| Extends: | Ember.Object |
|---|---|
| Uses: | Ember.ProxyMixin |
| Defined in: | packages/ember-runtime/lib/system/object_proxy.js:4 |
| Module: | ember |
Ember.ObjectProxy forwards all properties not defined by the proxy itself to a proxied content object.
object = Ember.Object.create({
name: 'Foo'
});
proxy = Ember.ObjectProxy.create({
content: object
});
// Access and change existing properties
proxy.get('name') // 'Foo'
proxy.set('name', 'Bar');
object.get('name') // 'Bar'
// Create new 'description' property on `object`
proxy.set('description', 'Foo is a whizboo baz');
object.get('description') // 'Foo is a whizboo baz' While content is unset, setting a property to be delegated will throw an Error.
proxy = Ember.ObjectProxy.create({
content: null,
flag: null
});
proxy.set('flag', true);
proxy.get('flag'); // true
proxy.get('foo'); // undefined
proxy.set('foo', 'data'); // throws Error Delegated properties can be bound to and will change when content is updated.
Computed properties on the proxy itself can depend on delegated properties.
ProxyWithComputedProperty = Ember.ObjectProxy.extend({
fullName: Ember.computed('firstName', 'lastName', function() {
var firstName = this.get('firstName'),
lastName = this.get('lastName');
if (firstName && lastName) {
return firstName + ' ' + lastName;
}
return firstName || lastName;
})
});
proxy = ProxyWithComputedProperty.create();
proxy.get('fullName'); // undefined
proxy.set('content', {
firstName: 'Tom', lastName: 'Dale'
}); // triggers property change for fullName on proxy
proxy.get('fullName'); // 'Tom Dale'
© 2017 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://emberjs.com/api/ember/2.15/classes/Ember.ObjectProxy