Function
readOnly (dependentKey) ComputedProperty public
Module:
@ember/object
Defined in packages/@ember/object/lib/computed/computed_macros.js:1076
Available since v1.5.0
import { readOnly } 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.oneWay
provides oneWay bindings, computed.readOnly
provides
a readOnly one way binding. Very often when using computed.oneWay
one does
not also want changes to propagate back up, as they will replace the value.
This prevents the reverse flow, and also throws an exception when it occurs.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import { set } from '@ember/object'; import { readOnly } 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'); // throws Exception // throw new EmberError('Cannot Set: nickName on: <User:ember27288>' );` teddy.firstName; // 'Teddy' |
Classic Class Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import EmberObject, { set } from '@ember/object'; import { readOnly } from '@ember/object/computed'; let User = EmberObject.extend({ firstName: null, lastName: null, nickName: readOnly('firstName') }); let teddy = User.create({ firstName: 'Teddy', lastName: 'Zeenny' }); teddy.nickName; // 'Teddy' set(teddy, 'nickName', 'TeddyBear'); // throws Exception // throw new EmberError('Cannot Set: nickName on: <User:ember27288>' );` teddy.firstName; // 'Teddy' |