From 7d96aa88e95836f81fc2331fd99ae5cef51e24a9 Mon Sep 17 00:00:00 2001 From: Philippe Lhoste Date: Thu, 6 Aug 2015 12:20:08 +0200 Subject: [PATCH] Fix onChange management - setValue is always silent (see #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 --- dist/selectize.js | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/dist/selectize.js b/dist/selectize.js index 7f01a87..57c20be 100755 --- a/dist/selectize.js +++ b/dist/selectize.js @@ -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)) @@ -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) { @@ -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); @@ -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 @@ -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);