Skip to content

Commit

Permalink
Pass index into computed array callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
DougPuchalski committed Aug 11, 2014
1 parent 0c9d8f3 commit e5aab14
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/ember-runtime/lib/computed/reduce_computed_macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,17 @@ export function min(dependentKey) {
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.
```javascript
function(item);
function(item, index);
```
Example
```javascript
var Hamster = Ember.Object.extend({
excitingChores: Ember.computed.map('chores', function(chore) {
excitingChores: Ember.computed.map('chores', function(chore, index) {
return chore.toUpperCase() + '!';
})
});
Expand All @@ -184,7 +185,7 @@ export function min(dependentKey) {
export function map(dependentKey, callback) {
var options = {
addedItem: function(array, item, changeMeta, instanceMeta) {
var mapped = callback.call(this, item);
var mapped = callback.call(this, item, changeMeta.index);
array.insertAt(changeMeta.index, mapped);
return array;
},
Expand Down Expand Up @@ -245,14 +246,15 @@ export var mapProperty = mapBy;
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.
```javascript
function(item);
function(item, index);
```
```javascript
var Hamster = Ember.Object.extend({
remainingChores: Ember.computed.filter('chores', function(chore) {
remainingChores: Ember.computed.filter('chores', function(chore, index) {
return !chore.done;
})
});
Expand Down Expand Up @@ -281,7 +283,7 @@ export function filter(dependentKey, callback) {
},

addedItem: function (array, item, changeMeta, instanceMeta) {
var match = !!callback.call(this, item);
var match = !!callback.call(this, item, changeMeta.index);
var filterIndex = instanceMeta.filteredArrayIndexes.addItem(changeMeta.index, match);

if (match) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ test("it maps simple unshifted properties", function() {
deepEqual(get(obj, 'mapped'), ['A', 'B'], "properties unshifted in sequence are mapped correctly");
});

test("it passes the index to the callback", function() {
var array = Ember.A(['a', 'b', 'c']);

run(function() {
obj = EmberObject.createWithMixins({
array: array,
mapped: computedMap('array', function (item, index) { return index; })
});
get(obj, 'mapped');
});

deepEqual(get(obj, 'mapped'), [0, 1, 2], "index is passed to callback correctly");
});

test("it maps objects", function() {
deepEqual(get(obj, 'mappedObjects'), [{ name: 'Robert'}, { name: 'Leanna' }]);

Expand Down Expand Up @@ -245,6 +259,20 @@ test("it filters according to the specified filter function", function() {
deepEqual(filtered, [2,4,6,8], "computedFilter filters by the specified function");
});

test("it passes the index to the callback", function() {
var array = Ember.A(['a', 'b', 'c']);

run(function() {
obj = EmberObject.createWithMixins({
array: array,
filtered: computedFilter('array', function (item, index) { return index === 1; })
});
get(obj, 'filtered');
});

deepEqual(get(obj, 'filtered'), ['b'], "index is passed to callback correctly");
});

test("it caches properly", function() {
var array = get(obj, 'array'),
filtered = get(obj, 'filtered');
Expand Down

0 comments on commit e5aab14

Please sign in to comment.