@@ -17,31 +17,37 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
17
17
scope . config = scope . config || { } ;
18
18
19
19
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
21
21
} ;
22
22
23
23
var toggle = function ( disabled ) {
24
24
disabled ? selectize . disable ( ) : selectize . enable ( ) ;
25
- }
25
+ } ;
26
26
27
27
var validate = function ( ) {
28
28
var isInvalid = ( scope . ngRequired ( ) || attrs . required || settings . required ) && isEmpty ( scope . ngModel ) ;
29
29
modelCtrl . $setValidity ( 'required' , ! isInvalid ) ;
30
30
} ;
31
31
32
32
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 ) {
35
39
var value = opt [ settings . valueField ] ;
36
40
selectize . removeOption ( value , true ) ;
37
41
}
38
42
} ) ;
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
39
48
40
- selectize . addOption ( curr , true ) ;
41
-
42
- selectize . refreshOptions ( false ) ; // updates results if user has entered a query
43
49
setSelectizeValue ( ) ;
44
- }
50
+ } ;
45
51
46
52
var setSelectizeValue = function ( ) {
47
53
validate ( ) ;
@@ -54,28 +60,48 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
54
60
if ( ! angular . equals ( selectize . items , scope . ngModel ) ) {
55
61
selectize . setValue ( scope . ngModel , true ) ;
56
62
}
57
- }
63
+ } ;
58
64
59
65
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 ] ;
63
69
}
64
- modelCtrl . $setViewValue ( value ) ;
70
+ modelCtrl . $setViewValue ( items ) ;
65
71
66
72
if ( scope . config . onChange ) {
67
73
scope . config . onChange . apply ( this , arguments ) ;
68
74
}
69
75
} ;
70
76
77
+ // User entered a new tag.
71
78
settings . onOptionAdd = function ( value , data ) {
72
- if ( scope . options . indexOf ( data ) === - 1 ) {
79
+ if ( scope . options . indexOf ( data ) === - 1 ) {
80
+ scope . _noUpdate = true ;
73
81
scope . options . push ( data ) ;
82
+ }
83
+ if ( scope . config . onOptionAdd ) {
84
+ scope . config . onOptionAdd . apply ( this , arguments ) ;
85
+ }
86
+ } ;
74
87
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 ;
77
96
}
78
97
}
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
+ }
79
105
} ;
80
106
81
107
settings . onInitialize = function ( ) {
0 commit comments