Function
computed (dependentKeys*, func) ComputedDecorator public
| Module: | @ember/object |
|---|
Defined in packages/@ember/-internals/metal/lib/computed.ts:880
import { computed } from '@ember/object'; - dependentKeys*
- String
- Optional dependent keys that trigger this computed property.
- func
- Function
- The computed property function.
- returns
- ComputedDecorator
- property decorator instance
This helper returns a new property descriptor that wraps the passed computed property function. You can use this helper to define properties with native decorator syntax, mixins, or via defineProperty().
Example:
import { computed, set } from '@ember/object';
class Person {
constructor() {
this.firstName = 'Betty';
this.lastName = 'Jones';
},
@computed('firstName', 'lastName')
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
let client = new Person();
client.fullName; // 'Betty Jones'
set(client, 'lastName', 'Fuller');
client.fullName; // 'Betty Fuller' Classic Class Example:
import EmberObject, { computed } from '@ember/object';
let Person = EmberObject.extend({
init() {
this._super(...arguments);
this.firstName = 'Betty';
this.lastName = 'Jones';
},
fullName: computed('firstName', 'lastName', function() {
return `${this.get('firstName')} ${this.get('lastName')}`;
})
});
let client = Person.create();
client.get('fullName'); // 'Betty Jones'
client.set('lastName', 'Fuller');
client.get('fullName'); // 'Betty Fuller' You can also provide a setter, either directly on the class using native class syntax, or by passing a hash with get and set functions.
Example:
import { computed, set } from '@ember/object';
class Person {
constructor() {
this.firstName = 'Betty';
this.lastName = 'Jones';
},
@computed('firstName', 'lastName')
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
set fullName(value) {
let [firstName, lastName] = value.split(/\s+/);
set(this, 'firstName', firstName);
set(this, 'lastName', lastName);
return value;
}
}
let client = new Person();
client.fullName; // 'Betty Jones'
set(client, 'lastName', 'Fuller');
client.fullName; // 'Betty Fuller' Classic Class Example:
import EmberObject, { computed } from '@ember/object';
let Person = EmberObject.extend({
init() {
this._super(...arguments);
this.firstName = 'Betty';
this.lastName = 'Jones';
},
fullName: computed('firstName', 'lastName', {
get(key) {
return `${this.get('firstName')} ${this.get('lastName')}`;
},
set(key, value) {
let [firstName, lastName] = value.split(/\s+/);
this.setProperties({ firstName, lastName });
return value;
}
})
});
let client = Person.create();
client.get('firstName'); // 'Betty'
client.set('fullName', 'Carroll Fuller');
client.get('firstName'); // 'Carroll' When passed as an argument, the set function should accept two parameters, key and value. The value returned from set will be the new value of the property.
Note: This is the preferred way to define computed properties when writing third-party libraries that depend on or use Ember, since there is no guarantee that the user will have prototype Extensions enabled.
The alternative syntax, with prototype extensions, might look like:
fullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
}.property('firstName', 'lastName') This form does not work with native decorators.
© 2020 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://api.emberjs.com/ember/3.25/functions/@ember%2Fobject/computed