Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(md-chips): appendChip disallows identical objects
Browse files Browse the repository at this point in the history
*  appendChip disallows appending an identical string or an exact object (duplicate object) more than once. It now disallows identical objects from being appended.

* appendChips object check loop optimized: md-chips' appendChip uses a filter to loop through items to search for an identical object to chip. This is inefficient since it will keep searching even after it finds a match. Now mdchips appendChip uses a some loop, which stops execution as soon as a match is found.

Fixes #4466. Closes #4479.
  • Loading branch information
JordanMajd authored and ThomasBurleson committed Oct 28, 2015
1 parent c577566 commit 03db13d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
24 changes: 24 additions & 0 deletions src/components/chips/chips.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,30 @@ describe('<md-chips>', function() {
expect(scope.appendChip.calls.mostRecent().args[0]).toBe('Apple');
});

it('should disallow identical object chips', function() {
var element = buildChips(CHIP_APPEND_TEMPLATE);
var ctrl = element.controller('mdChips');

ctrl.items = [{name: 'Apple', uppername: 'APPLE'}];

var chipObj = function(chip) {
return {
name: chip,
uppername: chip.toUpperCase()
};
};
scope.appendChip = jasmine.createSpy('appendChip').and.callFake(chipObj);

element.scope().$apply(function() {
ctrl.chipBuffer = 'Apple';
simulateInputEnterKey(ctrl);
});

expect(ctrl.items.length).toBe(1);
expect(scope.appendChip).toHaveBeenCalled();
expect(scope.appendChip.calls.mostRecent().args[0]).toBe('Apple');
});

it('should prevent the default when backspace is pressed', inject(function($mdConstant) {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
Expand Down
28 changes: 21 additions & 7 deletions src/components/chips/js/chipsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,27 @@ MdChipsCtrl.prototype.getAdjacentChipIndex = function(index) {
* call out to the md-on-append method, if provided
* @param newChip
*/
MdChipsCtrl.prototype.appendChip = function(newChip) {
if (this.useOnAppend && this.onAppend) {
newChip = this.onAppend({'$chip': newChip});
}
if (this.items.indexOf(newChip) + 1) return;
this.items.push(newChip);
};
MdChipsCtrl.prototype.appendChip = function(newChip) {

// If useOnAppend and onAppend function is provided call it.
if (this.useOnAppend && this.onAppend) {
newChip = this.onAppend({'$chip': newChip});
}

// If items contains identical object to newChip do not append
if(angular.isObject(newChip)){
var identical = this.items.some(function(item){
return angular.equals(newChip, item);
});
if(identical) return;
}

// If items contains newChip do not append
if (this.items.indexOf(newChip) + 1) return;

//add newChip to items
this.items.push(newChip);
};

/**
* Sets whether to use the md-on-append expression. This expression is
Expand Down

0 comments on commit 03db13d

Please sign in to comment.