Skip to content

Commit

Permalink
on-add, on-remove. Fixes mohbasheer#19
Browse files Browse the repository at this point in the history
  • Loading branch information
aecepoglu committed Dec 13, 2016
1 parent a465d87 commit 3b19d28
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
12 changes: 11 additions & 1 deletion dist/angular-chips.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@
scope.chips.list.push(data);
model.add(data);
}

if (scope.onAdd) {
scope.onAdd({$chip: data});
}
};

scope.chips.deleteChip = function(index) {
Expand All @@ -151,6 +155,10 @@
model.delete(index);
}

if (scope.onRemove) {
scope.onRemove({$chip: deletedChip});
}

return true;
}

Expand Down Expand Up @@ -256,7 +264,9 @@
* remove-chip="callback($chip)"
* Call back method should return true to remove or false for nothing
*/
removeChip: '&?'
removeChip: '&?',
onAdd: '&?',
onRemove: '&?'
},
transclude: true,
require: 'ngModel',
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-chips.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion src/js/directives/chips.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@
scope.chips.list.push(data);
model.add(data);
}

if (scope.onAdd) {
scope.onAdd({$chip: data});
}
};

scope.chips.deleteChip = function(index) {
Expand All @@ -148,6 +152,10 @@
model.delete(index);
}

if (scope.onRemove) {
scope.onRemove({$chip: deletedChip});
}

return true;
}

Expand Down Expand Up @@ -253,7 +261,9 @@
* remove-chip="callback($chip)"
* Call back method should return true to remove or false for nothing
*/
removeChip: '&?'
removeChip: '&?',
onAdd: '&?',
onRemove: '&?'
},
transclude: true,
require: 'ngModel',
Expand Down
1 change: 0 additions & 1 deletion test/basic_flow_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,4 @@ describe('Directive chips : Basic flow', function() {
var fun = function() { compile(angular.element(str))(scope) };
expect(fun).toThrow('should have only one chip-tmpl');
});

});
54 changes: 54 additions & 0 deletions test/callback_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

describe('Directive chips : callbacks', function() {

beforeEach(module('angular.chips'));

var element, scope, compile, template, isolateScope, timeout;
var input;

/*** Basic flow ***/

beforeEach(inject(function($rootScope, $injector) {
scope = $rootScope.$new();

scope.samples = ['Apple', 'Cisco', 'Verizon', 'Microsoft'];

scope.addCallback = function(x, y) {};

scope.removeCallback = function(x, y) {};

compile = $injector.get('$compile');
template = [
'<chips ng-model="samples" on-add="addCallback(\'a string\', $chip)"',
'on-remove="removeCallback(\'another string\', $chip)">',
'<chip-tmpl>',
'<chip-item>{{chip}}</chip-item>',
'</chip-tmpl>',
'<input chip-control></input>',
'</chips>'
].join("\n");

element = angular.element(template);
compile(element)(scope);
scope.$digest();
isolateScope = element.isolateScope();
input = element.find('input')[0];
}));

it('calls on-add callback when new chip is added', function() {
spyOn(scope, 'addCallback');

isolateScope.chips.addChip('new-chip-value');

expect(scope.addCallback).toHaveBeenCalledWith('a string', 'new-chip-value');
});

it('calls on-remove callback when a chip is removed', function() {
spyOn(scope, 'removeCallback');

isolateScope.chips.deleteChip(2);

expect(scope.removeCallback).toHaveBeenCalledWith('another string', 'Verizon');
});
});

0 comments on commit 3b19d28

Please sign in to comment.