Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Allow duplicate objects in ngRepeat. #2505

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions src/ng/directive/ngRepeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ var ngRepeatDirective = ['$parse', '$animator', function($parse, $animator) {
// - element: previous element.
// - index: position
var lastBlockMap = {};

//watch props
$scope.$watchCollection(rhs, function ngRepeatAction(collection){
var index, length,
Expand Down Expand Up @@ -233,33 +233,35 @@ var ngRepeatDirective = ['$parse', '$animator', function($parse, $animator) {
key = (collection === collectionKeys) ? index : collectionKeys[index];
value = collection[key];
trackById = trackByIdFn(key, value, index);
// FIXME: should we check that two objects with the same trackById are the same objects
if (!nextBlockMap.hasOwnProperty(trackById)) {
nextBlockMap[trackById] = [];
}
if(lastBlockMap.hasOwnProperty(trackById)) {
block = lastBlockMap[trackById]
delete lastBlockMap[trackById];
nextBlockMap[trackById] = block;
if (lastBlockMap[trackById].length===1) {
block = lastBlockMap[trackById][0];
delete lastBlockMap[trackById];
}
else {
block = lastBlockMap[trackById].splice(0,1)[0];
}
nextBlockMap[trackById].push(block);
nextBlockOrder[index] = block;
} else if (nextBlockMap.hasOwnProperty(trackById)) {
// restore lastBlockMap
forEach(nextBlockOrder, function(block) {
if (block && block.element) lastBlockMap[block.id] = block;
});
// This is a duplicate and we need to throw an error
throw new Error('Duplicates in a repeater are not allowed. Repeater: ' + expression +
' key: ' + trackById);
} else {
// new never before seen block
nextBlockOrder[index] = { id: trackById };
nextBlockMap[trackById] = false;
}
}

// remove existing items
for (key in lastBlockMap) {
if (lastBlockMap.hasOwnProperty(key)) {
block = lastBlockMap[key];
animate.leave(block.element);
block.element[0][NG_REMOVED] = true;
block.scope.$destroy();
for (index =0;index<lastBlockMap[key].length;++index) {
block = lastBlockMap[key][index];
animate.leave(block.element);
block.element[0][NG_REMOVED] = true;
block.scope.$destroy();
}
}
}

Expand Down Expand Up @@ -305,7 +307,7 @@ var ngRepeatDirective = ['$parse', '$animator', function($parse, $animator) {
cursor = clone;
block.scope = childScope;
block.element = clone;
nextBlockMap[block.id] = block;
nextBlockMap[block.id].push(block);
});
}
}
Expand Down
77 changes: 39 additions & 38 deletions test/ng/directive/ngRepeatSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,44 +457,45 @@ describe('ngRepeat', function() {
});


it('should throw error on adding existing duplicates and recover', function() {
scope.items = [a, a, a];
scope.$digest();
expect($exceptionHandler.errors.shift().message).
toEqual('Duplicates in a repeater are not allowed. Repeater: item in items key: object:003');

// recover
scope.items = [a];
scope.$digest();
var newElements = element.find('li');
expect(newElements.length).toEqual(1);
expect(newElements[0]).toEqual(lis[0]);

scope.items = [];
scope.$digest();
var newElements = element.find('li');
expect(newElements.length).toEqual(0);
});


it('should throw error on new duplicates and recover', function() {
scope.items = [d, d, d];
scope.$digest();
expect($exceptionHandler.errors.shift().message).
toEqual('Duplicates in a repeater are not allowed. Repeater: item in items key: object:009');

// recover
scope.items = [a];
scope.$digest();
var newElements = element.find('li');
expect(newElements.length).toEqual(1);
expect(newElements[0]).toEqual(lis[0]);

scope.items = [];
scope.$digest();
var newElements = element.find('li');
expect(newElements.length).toEqual(0);
});
it('should not throw error on adding existing duplicates', function() {
scope.items = [a, a, a];
scope.$digest();
expect($exceptionHandler.errors.shift()).toBeUndefined();
var newElements = element.find('li');
expect(newElements.length).toEqual(3);

scope.items.push(a);
scope.$digest();
newElements = element.find('li');
expect(newElements.length).toEqual(4);
});

it('should not throw error on removing a duplicate', function() {
scope.items = [a, a, a];
scope.$digest();
expect($exceptionHandler.errors.shift()).toBeUndefined();
var newElements = element.find('li');
expect(newElements.length).toEqual(3);

scope.items.splice(1,1);
scope.$digest();
newElements = element.find('li');
expect(newElements.length).toEqual(2);
});

it('should not throw error on moving a duplicate', function() {
scope.items = [a,a,a,c];
scope.$digest();
expect($exceptionHandler.errors.shift()).toBeUndefined();
var newElements = element.find('li');
expect(newElements.length).toEqual(4);

scope.items[2] = c;
scope.items[3] = a;
scope.$digest();
newElements = element.find('li');
expect(newElements.length).toEqual(4);
});


it('should reverse items when the collection is reversed', function() {
Expand Down