@@ -15,19 +15,14 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
15
15
var selectize ,
16
16
config = angular . extend ( { } , Selectize . defaults , selectizeConfig , scope . config ) ;
17
17
18
-
19
- //override to support checking empty arrays
20
18
modelCtrl . $isEmpty = function ( val ) {
21
- return ( ! val || ! val . length ) ;
19
+ return ( val === undefined || val === null || ! val . length ) ; //override to support checking empty arrays
22
20
}
23
21
24
-
25
22
function createItem ( input ) {
26
23
var data = { } ;
27
24
data [ config . labelField ] = input ;
28
25
data [ config . valueField ] = input ;
29
- data [ config . searchField ] = input ;
30
- data [ config . sortField ] = input ;
31
26
return data ;
32
27
}
33
28
@@ -39,66 +34,56 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
39
34
if ( ! config . required )
40
35
return true ;
41
36
42
- if ( modelCtrl . $isEmpty ( modelValue ) ) {
43
- selectize . $control . toggleClass ( 'ng-invalid' , true )
44
- return false ;
45
- } else {
46
- selectize . $control . toggleClass ( 'ng-invalid' , false )
47
- return true
48
- }
37
+ return ! modelCtrl . $isEmpty ( modelValue ) ;
49
38
} ;
50
39
51
40
config . onChange = function ( ) {
52
- if ( ! angular . equals ( selectize . items , scope . ngModel ) )
41
+ if ( ! angular . equals ( selectize . items , scope . ngModel ) )
53
42
modelCtrl . $setViewValue ( angular . copy ( selectize . items ) ) ;
54
43
}
55
44
45
+ function updateSelectize ( ) {
46
+ selectize . $control . toggleClass ( 'ng-valid' , modelCtrl . $valid )
47
+ selectize . $control . toggleClass ( 'ng-invalid' , modelCtrl . $invalid )
48
+ selectize . $control . toggleClass ( 'ng-dirty' , modelCtrl . $dirty )
49
+ selectize . $control . toggleClass ( 'ng-pristine' , modelCtrl . $pristine )
50
+
51
+ if ( ! angular . equals ( selectize . items , scope . ngModel ) )
52
+ selectize . setValue ( scope . ngModel )
53
+ }
54
+
56
55
config . onOptionAdd = function ( value , data ) {
57
56
if ( scope . options . indexOf ( data ) === - 1 )
58
57
scope . options . push ( data ) ;
59
58
}
60
-
61
- function updateSelectizeOptions ( ) {
62
- selectize . addOption ( scope . options )
63
- }
64
-
65
- function updateSelectizeValue ( ) {
66
- if ( ! angular . equals ( selectize . items , scope . ngModel ) )
67
- selectize . setValue ( scope . ngModel ) ;
68
- }
69
-
70
59
60
+ // ngModel (ie selected items) is included in this because if no options are specified, we
61
+ // need to create the corresponding options for the items to be visible
71
62
scope . options = scope . options || config . options || scope . ngModel || [ ] ;
72
63
73
-
74
64
scope . options = $ . map ( scope . options , function ( opt ) {
75
- if ( typeof opt === 'string' )
76
- return createItem ( opt )
77
- else
78
- return opt
79
- } )
65
+ return typeof opt === 'string' ? createItem ( opt ) : opt ;
66
+ } ) ;
80
67
81
68
config . onInitialize = function ( ) {
82
69
selectize = element [ 0 ] . selectize ;
83
-
84
70
selectize . addOption ( scope . options )
85
71
selectize . setValue ( scope . ngModel )
86
72
87
- scope . $watchCollection ( 'options' , updateSelectizeOptions ) ;
88
- scope . $watch ( 'ngModel' , updateSelectizeValue , true ) ;
89
- scope . $watch ( 'ngDisabled' , toggle )
73
+ scope . $watchCollection ( 'options' , selectize . addOption . bind ( selectize ) ) ;
74
+ scope . $watch ( 'ngModel' , updateSelectize ) ;
75
+ scope . $watch ( 'ngDisabled' , toggle ) ;
90
76
}
91
77
92
78
element . selectize ( config ) ;
93
79
94
80
element . on ( '$destroy' , function ( ) {
95
81
if ( selectize ) {
96
- selectize . destroy ( ) ;
97
- element = null ;
82
+ selectize . destroy ( ) ;
83
+ element = null ;
98
84
}
99
85
} ) ;
100
86
101
-
102
87
}
103
88
} ;
104
89
} ] ) ;
0 commit comments