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

Commit

Permalink
Give the render method the possibility to decide if the text should b…
Browse files Browse the repository at this point in the history
…e added as a new chip. Also don't clear the text if no chip was added.
  • Loading branch information
koenvd committed Dec 16, 2016
1 parent 49bb5a8 commit d46e754
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
18 changes: 13 additions & 5 deletions dist/angular-chips.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@
if (scope.render !== undefined && functionParam !== '') {
paramObj = {};
paramObj[functionParam] = data;
updatedData = scope.render(paramObj)
updatedData = scope.render(paramObj);
} else { updatedData = data }

if (!updatedData) {
return false;
}

if (isPromiseLike(updatedData)) {
updatedData.then(function(response) {
model.add(response);
Expand All @@ -119,6 +123,8 @@
scope.chips.list.push(data);
model.add(data);
}

return true;
};

scope.chips.deleteChip = function(index) {
Expand Down Expand Up @@ -412,8 +418,9 @@
function ChipControlLinkFun(scope, iElement, iAttrs, chipsCtrl) {
iElement.on('keypress', function(event) {
if (event.keyCode === 13 && event.target.value !== '') {
chipsCtrl.addChip(event.target.value);
event.target.value = "";
if (chipsCtrl.addChip(event.target.value)) {
event.target.value = "";
}
event.preventDefault();
}
});
Expand Down Expand Up @@ -445,8 +452,9 @@
ngModelCtrl.$render = function(event) {
if (!ngModelCtrl.$modelValue)
return;
chipsCtrl.addChip(ngModelCtrl.$modelValue);
iElement.val('');
if (chipsCtrl.addChip(ngModelCtrl.$modelValue)) {
iElement.val('');
}
}

iElement.on('focus', function() {
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.

8 changes: 7 additions & 1 deletion src/js/directives/chips.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,13 @@
if (scope.render !== undefined && functionParam !== '') {
paramObj = {};
paramObj[functionParam] = data;
updatedData = scope.render(paramObj)
updatedData = scope.render(paramObj);
} else { updatedData = data }

if (!updatedData) {
return false;
}

if (isPromiseLike(updatedData)) {
updatedData.then(function(response) {
model.add(response);
Expand All @@ -116,6 +120,8 @@
scope.chips.list.push(data);
model.add(data);
}

return true;
};

scope.chips.deleteChip = function(index) {
Expand Down
5 changes: 3 additions & 2 deletions src/js/directives/controls/chip_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
function ChipControlLinkFun(scope, iElement, iAttrs, chipsCtrl) {
iElement.on('keypress', function(event) {
if (event.keyCode === 13 && event.target.value !== '') {
chipsCtrl.addChip(event.target.value);
event.target.value = "";
if (chipsCtrl.addChip(event.target.value)) {
event.target.value = "";
}
event.preventDefault();
}
});
Expand Down
5 changes: 3 additions & 2 deletions src/js/directives/controls/ng_model_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
ngModelCtrl.$render = function(event) {
if (!ngModelCtrl.$modelValue)
return;
chipsCtrl.addChip(ngModelCtrl.$modelValue);
iElement.val('');
if (chipsCtrl.addChip(ngModelCtrl.$modelValue)) {
iElement.val('');
}
}

iElement.on('focus', function() {
Expand Down

0 comments on commit d46e754

Please sign in to comment.