Skip to content

Commit

Permalink
Fix onChange management
Browse files Browse the repository at this point in the history
- setValue is always silent (see machineboy2045#46)
- deactivate watch when updating the options list internally
- user entered options, when removed, must be removed from the list
- tested in http://plnkr.co/edit/2352rt
  • Loading branch information
PhiLhoSoft committed Aug 6, 2015
1 parent 58aff44 commit 7d96aa8
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions dist/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz

if (!angular.equals(selectize.items, scope.ngModel)) {
selectize.addOption(normalizeOptions(angular.copy(scope.ngModel)));
selectize.setValue(scope.ngModel);
selectize.setValue(scope.ngModel, true);
}
}

var onChange = config.onChange,
onOptionAdd = config.onOptionAdd;
onOptionAdd = config.onOptionAdd,
onOptionRemove = config.onOptionRemove;

config.onChange = function() {
if(scope.disableOnChange)
if (scope._disableOnChange)
return;

if (!angular.equals(selectize.items, scope.ngModel))
Expand All @@ -89,7 +90,8 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
};

config.onOptionAdd = function(value, data) {
if( scope.options.indexOf(data) === -1 ) {
if (scope.options.indexOf(data) === -1) {
scope._disableWatchOptions = true;
scope.options.push(data);

if (onOptionAdd) {
Expand All @@ -98,6 +100,24 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
}
};

config.onOptionRemove = function(value) {
var idx = -1;
for (var i = 0; i < scope.options.length; i++) {
if (scope.options[i][config.valueField] === value) {
idx = i;
break;
}
}
if (idx === -1)
return;
scope._disableWatchOptions = true;
scope.options.splice(idx, 1);

if (onOptionRemove) {
onOptionRemove.apply(this, arguments);
}
};

if (scope.options) {
// replace string options with generated options while retaining a reference to the same array
normalizeOptions(scope.options);
Expand All @@ -111,7 +131,7 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
config.onInitialize = function() {
selectize = element[0].selectize;
addOptions(scope.options);
selectize.setValue(scope.ngModel);
selectize.setValue(scope.ngModel, true);

//provides a way to access the selectize element from an
//angular controller
Expand All @@ -120,11 +140,16 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
}

scope.$watch('options', function() {
scope.disableOnChange = true;
if (scope._disableWatchOptions) {
scope._disableWatchOptions = false;
return;
}

scope._disableOnChange = true;
selectize.clearOptions();
addOptions(scope.options);
selectize.setValue(scope.ngModel);
scope.disableOnChange = false;
scope._disableOnChange = false;
}, true);

scope.$watchCollection('ngModel', updateSelectize);
Expand Down

0 comments on commit 7d96aa8

Please sign in to comment.