Class Ember.computed
publicThis helper returns a new property descriptor that wraps the passed
computed property function. You can use this helper to define properties
with mixins or via Ember.defineProperty()
.
If you pass a function as an argument, it will be used as a getter. A computed property defined in this way might look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
let Person = Ember.Object.extend({ init() { this._super(...arguments); this.firstName = 'Betty'; this.lastName = 'Jones'; }, fullName: Ember.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 pass a hash with two functions, get
and set
, as an
argument to provide both a getter and setter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
let Person = Ember.Object.extend({ init() { this._super(...arguments); this.firstName = 'Betty'; this.lastName = 'Jones'; }, fullName: Ember.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' |
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:
1 2 3 |
fullName: function() { return this.get('firstName') + ' ' + this.get('lastName'); }.property('firstName', 'lastName') |
alias (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:504
- dependentKey
- String
- returns
- Ember.ComputedProperty
- computed property which creates an alias to the original value for property.
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
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' |
and (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:428
- dependentKey
- String
- returns
- Ember.ComputedProperty
- computed property which performs a logical `and` on the values of all the original values for properties.
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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 |
bool (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:188
- dependentKey
- String
- returns
- Ember.ComputedProperty
- computed property which converts to boolean the original value for property
A computed property that converts the provided dependent property into a boolean value.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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 |
collect (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:591
- dependentKey
- String
- returns
- Ember.ComputedProperty
- computed property which maps values of all passed in properties to an array.
A computed property that returns the array of values for the provided dependent properties.
Example
1 2 3 4 5 6 7 8 9 10 |
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'] |
deprecatingAlias (dependentKey, options) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:620
Available since v1.7.0
- dependentKey
- String
- options
- Object
- Options for `Ember.deprecate`.
- returns
- Ember.ComputedProperty
- computed property which creates an alias with a deprecation to the original value for property.
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.
1 2 3 4 5 6 7 8 9 10 11 |
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 |
empty (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:58
Available since v1.6.0
- dependentKey
- String
- returns
- Ember.ComputedProperty
- computed property which negate the original value for property
A computed property that returns true if the value of the dependent property is null, an empty string, empty array, or empty function.
Example
1 2 3 4 5 6 7 8 9 10 11 |
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 |
equal (dependentKey, value) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:258
- dependentKey
- String
- value
- String|Number|Object
- returns
- Ember.ComputedProperty
- computed property which returns true if the original value for property is equal to the given value.
A computed property that returns true if the provided dependent property is equal to the given value.
Example
1 2 3 4 5 6 7 8 9 10 11 |
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 |
filter (dependentKey, callback) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:250
- dependentKey
- String
- callback
- Function
- returns
- Ember.ComputedProperty
- the filtered array
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.
1 |
function(item, index, array); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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'); // [] |
filterBy (dependentKey, propertyKey, value) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:315
- dependentKey
- String
- propertyKey
- String
- value
- *
- returns
- Ember.ComputedProperty
- the filtered array
Filters the array by the property and value
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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 }] |
gt (dependentKey, value) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:292
- dependentKey
- String
- value
- Number
- returns
- Ember.ComputedProperty
- computed property which returns true if the original value for property is greater than given value.
A computed property that returns true if the provided dependent property is greater than the provided value.
Example
1 2 3 4 5 6 7 8 9 10 11 |
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 |
gte (dependentKey, value) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:326
- dependentKey
- String
- value
- Number
- returns
- Ember.ComputedProperty
- computed property which returns true if the original value for property is greater or equal then given value.
A computed property that returns true if the provided dependent property is greater than or equal to the provided value.
Example
1 2 3 4 5 6 7 8 9 10 11 |
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 |
intersect (propertyKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:488
- propertyKey
- String
- returns
- Ember.ComputedProperty
- computes a new array with all the duplicated elements from the dependent arrays
A computed property which returns a new array with all the elements two or more dependent arrays have in common.
Example
1 2 3 4 5 6 7 8 |
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'] |
lt (dependentKey, value) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:360
- dependentKey
- String
- value
- Number
- returns
- Ember.ComputedProperty
- computed property which returns true if the original value for property is less then given value.
A computed property that returns true if the provided dependent property is less than the provided value.
Example
1 2 3 4 5 6 7 8 9 10 11 |
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 |
lte (dependentKey, value) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:394
- dependentKey
- String
- value
- Number
- returns
- Ember.ComputedProperty
- computed property which returns true if the original value for property is less or equal than given value.
A computed property that returns true if the provided dependent property is less than or equal to the provided value.
Example
1 2 3 4 5 6 7 8 9 10 11 |
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 |
map (dependentKey, callback) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:170
- dependentKey
- String
- callback
- Function
- returns
- Ember.ComputedProperty
- an array mapped via the callback
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.
1 |
function(item, index); |
Example
1 2 3 4 5 6 7 8 9 10 11 |
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!'] |
mapBy (dependentKey, propertyKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:210
- dependentKey
- String
- propertyKey
- String
- returns
- Ember.ComputedProperty
- an array mapped to the specified key
Returns an array mapped to the specified key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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] |
match (dependentKey, regexp) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:221
- dependentKey
- String
- regexp
- RegExp
- returns
- Ember.ComputedProperty
- computed property which match the original value for property against a given RegExp
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
1 2 3 4 5 6 7 8 9 10 11 |
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 |
max (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:80
- dependentKey
- String
- returns
- Ember.ComputedProperty
- computes the largest value in the dependentKey's array
A computed property that calculates the maximum value in the
dependent array. This will return -Infinity
when the dependent
array is empty.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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
.
min (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:125
- dependentKey
- String
- returns
- Ember.ComputedProperty
- computes the smallest value in the dependentKey's array
A computed property that calculates the minimum value in the
dependent array. This will return Infinity
when the dependent
array is empty.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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
.
none (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:123
- dependentKey
- String
- returns
- Ember.ComputedProperty
- computed property which returns true if original value for property is null or undefined.
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
1 2 3 4 5 6 7 8 9 10 11 |
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 |
not (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:157
- dependentKey
- String
- returns
- Ember.ComputedProperty
- computed property which returns inverse of the original value for property
A computed property that returns the inverse boolean value of the original value for the dependent property.
Example
1 2 3 4 5 6 7 8 9 |
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 |
notEmpty (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:92
- dependentKey
- String
- returns
- Ember.ComputedProperty
- computed property which returns true if original value for property is not empty.
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
1 2 3 4 5 6 7 8 9 |
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 |
oneWay (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:532
- dependentKey
- String
- returns
- Ember.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 |
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' |
or (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:467
- dependentKey
- String
- returns
- Ember.ComputedProperty
- computed property which performs a logical `or` on the values of all the original values for properties.
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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' |
readOnly (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:581
Available since v1.5.0
- dependentKey
- String
- returns
- Ember.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 |
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' |
reads (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/computed_macros.js:569
- dependentKey
- String
- returns
- Ember.ComputedProperty
- computed property which creates a one way computed property to the original value for property.
This is a more semantically meaningful alias of computed.oneWay
,
whose name is somewhat ambiguous as to which direction the data flows.
setDiff (setAProperty, setBProperty) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:543
- setAProperty
- String
- setBProperty
- String
- returns
- Ember.ComputedProperty
- computes a new array with all the items from the first dependent array that are not in the second dependent array
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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'] |
sort (itemsKey, sortDefinition) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:634
- itemsKey
- String
- sortDefinition
- String or Function
- a dependent key to an array of sort properties (add `:desc` to the arrays sort properties to sort descending) or a function to use when sorting
- returns
- Ember.ComputedProperty
- computes a new sorted array based on the sort property array or callback function
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:
1 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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 }] |
sum (dependentKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:65
Available since v1.4.0
- dependentKey
- String
- returns
- Ember.ComputedProperty
- computes the sum of all values in the dependentKey's array
A computed property that returns the sum of the values in the dependent array.
union (propertyKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:450
- propertyKey
- String
- returns
- Ember.ComputedProperty
- computes a new array with all the unique elements from the dependent array
A computed property which returns a new array with all the unique elements from one or more dependent arrays.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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'] |
uniq (propertyKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:354
- propertyKey
- String
- returns
- Ember.ComputedProperty
- computes a new array with all the unique elements from the dependent array
A computed property which returns a new array with all the unique elements from one or more dependent arrays.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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'] |
uniqBy (dependentKey, propertyKey) Ember.ComputedProperty public
Defined in packages/ember-runtime/lib/computed/reduce_computed_macros.js:403
- dependentKey
- String
- propertyKey
- String
- returns
- Ember.ComputedProperty
- computes a new array with all the unique elements from the dependent array
A computed property which returns a new array with all the unique elements from an array, with uniqueness determined by specific key.
Example
1 2 3 4 5 6 7 8 9 10 11 12 |
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' }] |