Skip to content

Commit abf1799

Browse files
cleanup. more ng-class support
1 parent d3f6314 commit abf1799

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

dist/selectize.js

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
1515
var selectize,
1616
config = angular.extend({}, Selectize.defaults, selectizeConfig, scope.config);
1717

18-
19-
//override to support checking empty arrays
2018
modelCtrl.$isEmpty = function(val){
21-
return (!val || !val.length);
19+
return (val === undefined || val === null || !val.length); //override to support checking empty arrays
2220
}
2321

24-
2522
function createItem(input) {
2623
var data = {};
2724
data[config.labelField] = input;
2825
data[config.valueField] = input;
29-
data[config.searchField] = input;
30-
data[config.sortField] = input;
3126
return data;
3227
}
3328

@@ -39,66 +34,56 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
3934
if(!config.required)
4035
return true;
4136

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);
4938
};
5039

5140
config.onChange = function(){
52-
if(!angular.equals(selectize.items, scope.ngModel))
41+
if( !angular.equals(selectize.items, scope.ngModel) )
5342
modelCtrl.$setViewValue( angular.copy(selectize.items) );
5443
}
5544

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+
5655
config.onOptionAdd = function(value, data) {
5756
if( scope.options.indexOf(data) === -1 )
5857
scope.options.push(data);
5958
}
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-
7059

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
7162
scope.options = scope.options || config.options || scope.ngModel || [];
7263

73-
7464
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+
});
8067

8168
config.onInitialize = function(){
8269
selectize = element[0].selectize;
83-
8470
selectize.addOption(scope.options)
8571
selectize.setValue(scope.ngModel)
8672

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);
9076
}
9177

9278
element.selectize(config);
9379

9480
element.on('$destroy', function() {
9581
if (selectize) {
96-
selectize.destroy();
97-
element = null;
82+
selectize.destroy();
83+
element = null;
9884
}
9985
});
10086

101-
10287
}
10388
};
10489
}]);

0 commit comments

Comments
 (0)