Skip to content

Commit daa7dd0

Browse files
committed
Treat user entered options differently from original options
Or programmatically changed options. Fixes issue machineboy2045#69. Also fixes machineboy2045#110... Demo at http://plnkr.co/edit/2352rt
1 parent d0d3fbf commit daa7dd0

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

dist/angular-selectize.js

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,37 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
1717
scope.config = scope.config || {};
1818

1919
var isEmpty = function(val) {
20-
return val === undefined || val === null || !val.length; //support checking empty arrays
20+
return val === undefined || val === null || val.length === 0; // support checking empty arrays / strings
2121
};
2222

2323
var toggle = function(disabled) {
2424
disabled ? selectize.disable() : selectize.enable();
25-
}
25+
};
2626

2727
var validate = function() {
2828
var isInvalid = (scope.ngRequired() || attrs.required || settings.required) && isEmpty(scope.ngModel);
2929
modelCtrl.$setValidity('required', !isInvalid);
3030
};
3131

3232
var setSelectizeOptions = function(curr, prev) {
33-
angular.forEach(prev, function(opt){
34-
if(curr.indexOf(opt) === -1){
33+
if (scope._noUpdate) { // Internal changes to scope.options, eschew the watch mechanism
34+
scope._noUpdate = false;
35+
return;
36+
}
37+
angular.forEach(prev, function(opt) {
38+
if (curr.indexOf(opt) === -1) {
3539
var value = opt[settings.valueField];
3640
selectize.removeOption(value, true);
3741
}
3842
});
43+
angular.forEach(curr, function(opt) {
44+
selectize.registerOption(opt);
45+
});
46+
selectize.lastQuery = undefined; // Hack because of a Selectize bug...
47+
selectize.refreshOptions(false); // Update the content of the drop-down
3948

40-
selectize.addOption(curr, true);
41-
42-
selectize.refreshOptions(false); // updates results if user has entered a query
4349
setSelectizeValue();
44-
}
50+
};
4551

4652
var setSelectizeValue = function() {
4753
validate();
@@ -54,28 +60,48 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
5460
if (!angular.equals(selectize.items, scope.ngModel)) {
5561
selectize.setValue(scope.ngModel, true);
5662
}
57-
}
63+
};
5864

5965
settings.onChange = function(value) {
60-
var value = angular.copy(selectize.items);
61-
if (settings.maxItems == 1) {
62-
value = value[0]
66+
var items = angular.copy(selectize.items);
67+
if (settings.maxItems === 1) {
68+
items = items[0];
6369
}
64-
modelCtrl.$setViewValue( value );
70+
modelCtrl.$setViewValue(items);
6571

6672
if (scope.config.onChange) {
6773
scope.config.onChange.apply(this, arguments);
6874
}
6975
};
7076

77+
// User entered a new tag.
7178
settings.onOptionAdd = function(value, data) {
72-
if( scope.options.indexOf(data) === -1 ) {
79+
if (scope.options.indexOf(data) === -1) {
80+
scope._noUpdate = true;
7381
scope.options.push(data);
82+
}
83+
if (scope.config.onOptionAdd) {
84+
scope.config.onOptionAdd.apply(this, arguments);
85+
}
86+
};
7487

75-
if (scope.config.onOptionAdd) {
76-
scope.config.onOptionAdd.apply(this, arguments);
88+
// User removed a tag they entered.
89+
// Note: it is not called if persist is true.
90+
settings.onOptionRemove = function(value) {
91+
var idx = -1;
92+
for (var i = 0; i < scope.options.length; i++) {
93+
if (scope.options[i][scope.config.valueField] === value) {
94+
idx = i;
95+
break;
7796
}
7897
}
98+
if (idx !== -1 ) {
99+
scope._noUpdate = true;
100+
scope.options.splice(idx, 1);
101+
}
102+
if (scope.config.onOptionRemove) {
103+
scope.config.onOptionRemove.apply(this, arguments);
104+
}
79105
};
80106

81107
settings.onInitialize = function() {

0 commit comments

Comments
 (0)