Class ComputedProperty

public
Defined in: packages/ember-metal/lib/computed.js:25
Module: @ember/object

meta (meta) public

Module: @ember/object
meta
Object

In some cases, you may want to annotate computed properties with additional metadata about how they function or what values they operate on. For example, computed property functions may close over variables that are then no longer available for introspection.

You can pass a hash of these values to a computed property like this:

import { computed } from '@ember/object';
import Person from 'my-app/utils/person';

person: computed(function() {
  let personId = this.get('personId');
  return Person.create({ id: personId });
}).meta({ type: Person })

The hash that you pass to the meta() function will be saved on the computed property descriptor under the _meta key. Ember runtime exposes a public API for retrieving these values from classes, via the metaForProperty() function.

property (path) ComputedProperty public

Module: @ember/object
path
String
zero or more property paths
returns
ComputedProperty
this

Sets the dependent keys on this computed property. Pass any number of arguments containing key paths that this computed property depends on.

import EmberObject, { computed } from '@ember/object';

let President = EmberObject.extend({
  fullName: computed('firstName', 'lastName', function() {
    return this.get('firstName') + ' ' + this.get('lastName');

    // Tell Ember that this computed property depends on firstName
    // and lastName
  })
});

let president = President.create({
  firstName: 'Barack',
  lastName: 'Obama'
});

president.get('fullName'); // 'Barack Obama'

readOnly ComputedProperty public

Module: @ember/object
returns
ComputedProperty
this

Call on a computed property to set it into read-only mode. When in this mode the computed property will throw an error when set.

import EmberObject, { computed } from '@ember/object';

let Person = EmberObject.extend({
  guid: computed(function() {
    return 'guid-guid-guid';
  }).readOnly()
});

let person = Person.create();

person.set('guid', 'new-guid'); // will throw an exception

volatile ComputedProperty public

Module: @ember/object
returns
ComputedProperty
this

Call on a computed property to set it into non-cached mode. When in this mode the computed property will not automatically cache the return value.

It also does not automatically fire any change events. You must manually notify any changes if you want to observe this property.

Dependency keys have no effect on volatile properties as they are for cache invalidation and notification when cached value is invalidated.

import EmberObject, { computed } from '@ember/object';

let outsideService = EmberObject.extend({
  value: computed(function() {
    return OutsideService.getValue();
  }).volatile()
}).create();

© 2020 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://api.emberjs.com/ember/2.18/classes/ComputedProperty/methods