Defined in: | packages/ember-metal/lib/computed.js:437 |
---|---|
Module: | ember |
Defined in packages/ember-runtime/lib/computed/computed_macros.js:504
Creates a new property that is an alias for another property on an object. Calls to get
or set
this property behave as though they were called on the original property.
let Person = Ember.Object.extend({ name: 'Alex Matchneer', nomen: Ember.computed.alias('name') }); let alex = Person.create(); alex.get('nomen'); // 'Alex Matchneer' alex.get('name'); // 'Alex Matchneer' alex.set('nomen', '@machty'); alex.get('name'); // '@machty'
Defined in packages/ember-runtime/lib/computed/computed_macros.js:428
A computed property that performs a logical and
on the original values for the provided dependent properties.
You may pass in more than two properties and even use property brace expansion. The computed property will return the first falsy value or last truthy value just like JavaScript's &&
operator.
Example
let Hamster = Ember.Object.extend({ readyForCamp: Ember.computed.and('hasTent', 'hasBackpack'), readyForHike: Ember.computed.and('hasWalkingStick', 'hasBackpack') }); let tomster = Hamster.create(); tomster.get('readyForCamp'); // false tomster.set('hasTent', true); tomster.get('readyForCamp'); // false tomster.set('hasBackpack', true); tomster.get('readyForCamp'); // true tomster.set('hasBackpack', 'Yes'); tomster.get('readyForCamp'); // 'Yes' tomster.set('hasWalkingStick', null); tomster.get('readyForHike'); // null
Defined in packages/ember-runtime/lib/computed/computed_macros.js:188
A computed property that converts the provided dependent property into a boolean value.
let Hamster = Ember.Object.extend({ hasBananas: Ember.computed.bool('numBananas') }); let hamster = Hamster.create(); hamster.get('hasBananas'); // false hamster.set('numBananas', 0); hamster.get('hasBananas'); // false hamster.set('numBananas', 1); hamster.get('hasBananas'); // true hamster.set('numBananas', null); hamster.get('hasBananas'); // false
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:590
A computed property that returns the array of values for the provided dependent properties.
Example
let Hamster = Ember.Object.extend({ clothes: Ember.computed.collect('hat', 'shirt') }); let hamster = Hamster.create(); hamster.get('clothes'); // [null, null] hamster.set('hat', 'Camp Hat'); hamster.set('shirt', 'Camp Shirt'); hamster.get('clothes'); // ['Camp Hat', 'Camp Shirt']
Defined in packages/ember-runtime/lib/computed/computed_macros.js:620
Creates a new property that is an alias for another property on an object. Calls to get
or set
this property behave as though they were called on the original property, but also print a deprecation warning.
let Hamster = Ember.Object.extend({ bananaCount: Ember.computed.deprecatingAlias('cavendishCount', { id: 'hamster.deprecate-banana', until: '3.0.0' }) }); let hamster = Hamster.create(); hamster.set('bananaCount', 5); // Prints a deprecation warning. hamster.get('cavendishCount'); // 5
Defined in packages/ember-runtime/lib/computed/computed_macros.js:58
A computed property that returns true if the value of the dependent property is null, an empty string, empty array, or empty function.
Example
let ToDoList = Ember.Object.extend({ isDone: Ember.computed.empty('todos') }); let todoList = ToDoList.create({ todos: ['Unit Test', 'Documentation', 'Release'] }); todoList.get('isDone'); // false todoList.get('todos').clear(); todoList.get('isDone'); // true
Defined in packages/ember-runtime/lib/computed/computed_macros.js:258
A computed property that returns true if the provided dependent property is equal to the given value.
Example
let Hamster = Ember.Object.extend({ satisfied: Ember.computed.equal('percentCarrotsEaten', 100) }); let hamster = Hamster.create(); hamster.get('satisfied'); // false hamster.set('percentCarrotsEaten', 100); hamster.get('satisfied'); // true hamster.set('percentCarrotsEaten', 50); hamster.get('satisfied'); // false
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:249
Filters the array by the callback.
The callback method you provide should have the following signature. item
is the current item in the iteration. index
is the integer index of the current item in the iteration. array
is the dependant array itself.
function(item, index, array);
let Hamster = Ember.Object.extend({ remainingChores: Ember.computed.filter('chores', function(chore, index, array) { return !chore.done; }) }); let hamster = Hamster.create({ chores: [ { name: 'cook', done: true }, { name: 'clean', done: true }, { name: 'write more unit tests', done: false } ] }); hamster.get('remainingChores'); // [{name: 'write more unit tests', done: false}]
You can also use @each.property
in your dependent key, the callback will still use the underlying array:
let Hamster = Ember.Object.extend({ remainingChores: Ember.computed.filter('chores.@each.done', function(chore, index, array) { return !chore.get('done'); }) }); let hamster = Hamster.create({ chores: Ember.A([ Ember.Object.create({ name: 'cook', done: true }), Ember.Object.create({ name: 'clean', done: true }), Ember.Object.create({ name: 'write more unit tests', done: false }) ]) }); hamster.get('remainingChores'); // [{name: 'write more unit tests', done: false}] hamster.get('chores').objectAt(2).set('done', true); hamster.get('remainingChores'); // []
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:314
Filters the array by the property and value
let Hamster = Ember.Object.extend({ remainingChores: Ember.computed.filterBy('chores', 'done', false) }); let hamster = Hamster.create({ chores: [ { name: 'cook', done: true }, { name: 'clean', done: true }, { name: 'write more unit tests', done: false } ] }); hamster.get('remainingChores'); // [{ name: 'write more unit tests', done: false }]
Defined in packages/ember-runtime/lib/computed/computed_macros.js:292
A computed property that returns true if the provided dependent property is greater than the provided value.
Example
let Hamster = Ember.Object.extend({ hasTooManyBananas: Ember.computed.gt('numBananas', 10) }); let hamster = Hamster.create(); hamster.get('hasTooManyBananas'); // false hamster.set('numBananas', 3); hamster.get('hasTooManyBananas'); // false hamster.set('numBananas', 11); hamster.get('hasTooManyBananas'); // true
Defined in packages/ember-runtime/lib/computed/computed_macros.js:326
A computed property that returns true if the provided dependent property is greater than or equal to the provided value.
Example
let Hamster = Ember.Object.extend({ hasTooManyBananas: Ember.computed.gte('numBananas', 10) }); let hamster = Hamster.create(); hamster.get('hasTooManyBananas'); // false hamster.set('numBananas', 3); hamster.get('hasTooManyBananas'); // false hamster.set('numBananas', 10); hamster.get('hasTooManyBananas'); // true
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:487
A computed property which returns a new array with all the elements two or more dependent arrays have in common.
Example
let obj = Ember.Object.extend({ friendsInCommon: Ember.computed.intersect('adaFriends', 'charlesFriends') }).create({ adaFriends: ['Charles Babbage', 'John Hobhouse', 'William King', 'Mary Somerville'], charlesFriends: ['William King', 'Mary Somerville', 'Ada Lovelace', 'George Peacock'] }); obj.get('friendsInCommon'); // ['William King', 'Mary Somerville']
Defined in packages/ember-runtime/lib/computed/computed_macros.js:360
A computed property that returns true if the provided dependent property is less than the provided value.
Example
let Hamster = Ember.Object.extend({ needsMoreBananas: Ember.computed.lt('numBananas', 3) }); let hamster = Hamster.create(); hamster.get('needsMoreBananas'); // true hamster.set('numBananas', 3); hamster.get('needsMoreBananas'); // false hamster.set('numBananas', 2); hamster.get('needsMoreBananas'); // true
Defined in packages/ember-runtime/lib/computed/computed_macros.js:394
A computed property that returns true if the provided dependent property is less than or equal to the provided value.
Example
let Hamster = Ember.Object.extend({ needsMoreBananas: Ember.computed.lte('numBananas', 3) }); let hamster = Hamster.create(); hamster.get('needsMoreBananas'); // true hamster.set('numBananas', 5); hamster.get('needsMoreBananas'); // false hamster.set('numBananas', 3); hamster.get('needsMoreBananas'); // true
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:169
Returns an array mapped via the callback
The callback method you provide should have the following signature. item
is the current item in the iteration. index
is the integer index of the current item in the iteration.
function(item, index);
Example
let Hamster = Ember.Object.extend({ excitingChores: Ember.computed.map('chores', function(chore, index) { return chore.toUpperCase() + '!'; }) }); let hamster = Hamster.create({ chores: ['clean', 'write more unit tests'] }); hamster.get('excitingChores'); // ['CLEAN!', 'WRITE MORE UNIT TESTS!']
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:209
Returns an array mapped to the specified key.
let Person = Ember.Object.extend({ childAges: Ember.computed.mapBy('children', 'age') }); let lordByron = Person.create({ children: [] }); lordByron.get('childAges'); // [] lordByron.get('children').pushObject({ name: 'Augusta Ada Byron', age: 7 }); lordByron.get('childAges'); // [7] lordByron.get('children').pushObjects([{ name: 'Allegra Byron', age: 5 }, { name: 'Elizabeth Medora Leigh', age: 8 }]); lordByron.get('childAges'); // [7, 5, 8]
Defined in packages/ember-runtime/lib/computed/computed_macros.js:221
A computed property which matches the original value for the dependent property against a given RegExp, returning true
if the value matches the RegExp and false
if it does not.
Example
let User = Ember.Object.extend({ hasValidEmail: Ember.computed.match('email', /^.+@.+\..+$/) }); let user = User.create({loggedIn: false}); user.get('hasValidEmail'); // false user.set('email', ''); user.get('hasValidEmail'); // false user.set('email', 'ember_hamster@example.com'); user.get('hasValidEmail'); // true
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:79
A computed property that calculates the maximum value in the dependent array. This will return -Infinity
when the dependent array is empty.
let Person = Ember.Object.extend({ childAges: Ember.computed.mapBy('children', 'age'), maxChildAge: Ember.computed.max('childAges') }); let lordByron = Person.create({ children: [] }); lordByron.get('maxChildAge'); // -Infinity lordByron.get('children').pushObject({ name: 'Augusta Ada Byron', age: 7 }); lordByron.get('maxChildAge'); // 7 lordByron.get('children').pushObjects([{ name: 'Allegra Byron', age: 5 }, { name: 'Elizabeth Medora Leigh', age: 8 }]); lordByron.get('maxChildAge'); // 8
If the types of the arguments are not numbers, they will be converted to numbers and the type of the return value will always be Number
. For example, the max of a list of Date objects will be the highest timestamp as a Number
. This behavior is consistent with Math.max
.
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:124
A computed property that calculates the minimum value in the dependent array. This will return Infinity
when the dependent array is empty.
let Person = Ember.Object.extend({ childAges: Ember.computed.mapBy('children', 'age'), minChildAge: Ember.computed.min('childAges') }); let lordByron = Person.create({ children: [] }); lordByron.get('minChildAge'); // Infinity lordByron.get('children').pushObject({ name: 'Augusta Ada Byron', age: 7 }); lordByron.get('minChildAge'); // 7 lordByron.get('children').pushObjects([{ name: 'Allegra Byron', age: 5 }, { name: 'Elizabeth Medora Leigh', age: 8 }]); lordByron.get('minChildAge'); // 5
If the types of the arguments are not numbers, they will be converted to numbers and the type of the return value will always be Number
. For example, the min of a list of Date objects will be the lowest timestamp as a Number
. This behavior is consistent with Math.min
.
Defined in packages/ember-runtime/lib/computed/computed_macros.js:123
A computed property that returns true if the value of the dependent property is null or undefined. This avoids errors from JSLint complaining about use of ==, which can be technically confusing.
Example
let Hamster = Ember.Object.extend({ isHungry: Ember.computed.none('food') }); let hamster = Hamster.create(); hamster.get('isHungry'); // true hamster.set('food', 'Banana'); hamster.get('isHungry'); // false hamster.set('food', null); hamster.get('isHungry'); // true
Defined in packages/ember-runtime/lib/computed/computed_macros.js:157
A computed property that returns the inverse boolean value of the original value for the dependent property.
Example
let User = Ember.Object.extend({ isAnonymous: Ember.computed.not('loggedIn') }); let user = User.create({loggedIn: false}); user.get('isAnonymous'); // true user.set('loggedIn', true); user.get('isAnonymous'); // false
Defined in packages/ember-runtime/lib/computed/computed_macros.js:92
A computed property that returns true if the value of the dependent property is NOT null, an empty string, empty array, or empty function.
Example
let Hamster = Ember.Object.extend({ hasStuff: Ember.computed.notEmpty('backpack') }); let hamster = Hamster.create({ backpack: ['Food', 'Sleeping Bag', 'Tent'] }); hamster.get('hasStuff'); // true hamster.get('backpack').clear(); // [] hamster.get('hasStuff'); // false
Defined in packages/ember-runtime/lib/computed/computed_macros.js:532
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
let User = Ember.Object.extend({ firstName: null, lastName: null, nickName: Ember.computed.oneWay('firstName') }); let teddy = User.create({ firstName: 'Teddy', lastName: 'Zeenny' }); teddy.get('nickName'); // 'Teddy' teddy.set('nickName', 'TeddyBear'); // 'TeddyBear' teddy.get('firstName'); // 'Teddy'
Defined in packages/ember-runtime/lib/computed/computed_macros.js:467
A computed property which performs a logical or
on the original values for the provided dependent properties.
You may pass in more than two properties and even use property brace expansion. The computed property will return the first truthy value or last falsy value just like JavaScript's ||
operator.
Example
let Hamster = Ember.Object.extend({ readyForRain: Ember.computed.or('hasJacket', 'hasUmbrella'), readyForBeach: Ember.computed.or('{hasSunscreen,hasUmbrella}') }); let tomster = Hamster.create(); tomster.get('readyForRain'); // undefined tomster.set('hasUmbrella', true); tomster.get('readyForRain'); // true tomster.set('hasJacket', 'Yes'); tomster.get('readyForRain'); // 'Yes' tomster.set('hasSunscreen', 'Check'); tomster.get('readyForBeach'); // 'Check'
Defined in packages/ember-runtime/lib/computed/computed_macros.js:581
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
let User = Ember.Object.extend({ firstName: null, lastName: null, nickName: Ember.computed.readOnly('firstName') }); let teddy = User.create({ firstName: 'Teddy', lastName: 'Zeenny' }); teddy.get('nickName'); // 'Teddy' teddy.set('nickName', 'TeddyBear'); // throws Exception // throw new Ember.Error('Cannot Set: nickName on: <User:ember27288>' );` teddy.get('firstName'); // 'Teddy'
Defined in packages/ember-runtime/lib/computed/computed_macros.js:569
This is a more semantically meaningful alias of computed.oneWay
, whose name is somewhat ambiguous as to which direction the data flows.
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:542
A computed property which returns a new array with all the properties from the first dependent array that are not in the second dependent array.
Example
let Hamster = Ember.Object.extend({ likes: ['banana', 'grape', 'kale'], wants: Ember.computed.setDiff('likes', 'fruits') }); let hamster = Hamster.create({ fruits: [ 'grape', 'kale', ] }); hamster.get('wants'); // ['banana']
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:633
A computed property which returns a new array with all the properties from the first dependent array sorted based on a property or sort function.
The callback method you provide should have the following signature:
function(itemA, itemB);
itemA
the first item to compare.itemB
the second item to compare.This function should return negative number (e.g. -1
) when itemA
should come before itemB
. It should return positive number (e.g. 1
) when itemA
should come after itemB
. If the itemA
and itemB
are equal this function should return 0
.
Therefore, if this function is comparing some numeric values, simple itemA - itemB
or itemA.get( 'foo' ) - itemB.get( 'foo' )
can be used instead of series of if
.
Example
let ToDoList = Ember.Object.extend({ // using standard ascending sort todosSorting: ['name'], sortedTodos: Ember.computed.sort('todos', 'todosSorting'), // using descending sort todosSortingDesc: ['name:desc'], sortedTodosDesc: Ember.computed.sort('todos', 'todosSortingDesc'), // using a custom sort function priorityTodos: Ember.computed.sort('todos', function(a, b){ if (a.priority > b.priority) { return 1; } else if (a.priority < b.priority) { return -1; } return 0; }) }); let todoList = ToDoList.create({todos: [ { name: 'Unit Test', priority: 2 }, { name: 'Documentation', priority: 3 }, { name: 'Release', priority: 1 } ]}); todoList.get('sortedTodos'); // [{ name:'Documentation', priority:3 }, { name:'Release', priority:1 }, { name:'Unit Test', priority:2 }] todoList.get('sortedTodosDesc'); // [{ name:'Unit Test', priority:2 }, { name:'Release', priority:1 }, { name:'Documentation', priority:3 }] todoList.get('priorityTodos'); // [{ name:'Release', priority:1 }, { name:'Unit Test', priority:2 }, { name:'Documentation', priority:3 }]
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:64
A computed property that returns the sum of the values in the dependent array.
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:449
A computed property which returns a new array with all the unique elements from one or more dependent arrays.
Example
let Hamster = Ember.Object.extend({ uniqueFruits: Ember.computed.union('fruits', 'vegetables') }); let hamster = Hamster.create({ fruits: [ 'banana', 'grape', 'kale', 'banana', 'tomato' ], vegetables: [ 'tomato', 'carrot', 'lettuce' ] }); hamster.get('uniqueFruits'); // ['banana', 'grape', 'kale', 'tomato', 'carrot', 'lettuce']
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:353
A computed property which returns a new array with all the unique elements from one or more dependent arrays.
Example
let Hamster = Ember.Object.extend({ uniqueFruits: Ember.computed.uniq('fruits') }); let hamster = Hamster.create({ fruits: [ 'banana', 'grape', 'kale', 'banana' ] }); hamster.get('uniqueFruits'); // ['banana', 'grape', 'kale']
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:402
A computed property which returns a new array with all the unique elements from an array, with uniqueness determined by specific key.
Example
let Hamster = Ember.Object.extend({ uniqueFruits: Ember.computed.uniqBy('fruits', 'id') }); let hamster = Hamster.create({ fruits: [ { id: 1, 'banana' }, { id: 2, 'grape' }, { id: 3, 'peach' }, { id: 1, 'banana' } ] }); hamster.get('uniqueFruits'); // [ { id: 1, 'banana' }, { id: 2, 'grape' }, { id: 3, 'peach' }]
© 2017 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://emberjs.com/api/ember/2.15/classes/Ember.computed/methods