Function
oneWay (dependentKey) ComputedProperty public
Module:
@ember/object
Defined in packages/@ember/object/lib/computed/computed_macros.js:990
import { oneWay } from '@ember/object/computed'; |
- dependentKey
- String
- returns
- ComputedProperty
- computed property which creates a one way computed property to the original value for property.
Where computed.alias
aliases get
and set
, and allows for bidirectional
data flow, computed.oneWay
only provides an aliased get
. The set
will
not mutate the upstream property, rather causes the current property to become
the value set. This causes the downstream property to permanently diverge from
the upstream property.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import { set } from '@ember/object'; import { oneWay }from '@ember/object/computed'; class User { constructor(firstName, lastName) { set(this, 'firstName', firstName); set(this, 'lastName', lastName); } nickName; } let teddy = new User('Teddy', 'Zeenny'); teddy.nickName; // 'Teddy' set(teddy, 'nickName', 'TeddyBear'); teddy.firstName; // 'Teddy' teddy.nickName; // 'TeddyBear' |
Classic Class Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import EmberObject, { set } from '@ember/object'; import { oneWay } from '@ember/object/computed'; let User = EmberObject.extend({ firstName: null, lastName: null, nickName: oneWay('firstName') }); let teddy = User.create({ firstName: 'Teddy', lastName: 'Zeenny' }); teddy.nickName; // 'Teddy' set(teddy, 'nickName', 'TeddyBear'); // 'TeddyBear' teddy.firstName; // 'Teddy' teddy.nickName; // 'TeddyBear' |