Skip to content

Commit

Permalink
Closes #46 - prevent onChange getting triggered too many times
Browse files Browse the repository at this point in the history
  • Loading branch information
machineboy2045 committed Aug 4, 2015
1 parent 1b0f02c commit 719a3d2
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dist/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
onOptionAdd = config.onOptionAdd;

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

if (!angular.equals(selectize.items, scope.ngModel))
scope.$evalAsync(function() {
var value = angular.copy(selectize.items);
Expand Down Expand Up @@ -106,9 +109,11 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
}

scope.$watch('options', function() {
scope.disableOnChange = true;
selectize.clearOptions();
selectize.addOption(scope.options);
selectize.setValue(scope.ngModel);
scope.disableOnChange = false;

This comment has been minimized.

Copy link
@PhiLhoSoft

PhiLhoSoft Aug 5, 2015

Contributor

Why didn't you use the 'silent' option of setValue? Seems simpler / as designed. Do I miss something (I haven't tested).

This comment has been minimized.

Copy link
@machineboy2045

machineboy2045 Aug 5, 2015

Author Owner

@PhiLhoSoft I had an older version of selectize.js that didn't include that option. So I updated and tried it. Looks like onChange() still gets triggered. Maybe clearOptions() or addOption() is responsible? http://plnkr.co/edit/eHALnB?p=preview

This comment has been minimized.

Copy link
@PhiLhoSoft

PhiLhoSoft Aug 5, 2015

Contributor

I debugged, and indeed, that's clearOptions() which is the culprit. It calls clear() without parameters, while the latter can accept a silent parameter. But clearOptions doesn't have such parameter, alas. Would need to raise an issue on Selectize, but they accumulate as the project owner is busy elsewhere.
So your workaround is fine, after all... 😄

For single selection, the culprit is in updateSelectize, there is still a setValue call without the silent parameter.
It should be added, as I see the double onChange call with your new code.

}, true);

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

0 comments on commit 719a3d2

Please sign in to comment.