Skip to content

Commit 773a2a4

Browse files
author
Dean Sofer
committed
fix(select2): Updating Select2 version support
1 parent 992477a commit 773a2a4

File tree

2 files changed

+96
-64
lines changed

2 files changed

+96
-64
lines changed

modules/directives/select2/select2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ angular.module('ui.directives').directive('uiSelect2', ['ui.config', '$timeout',
8383
if (!isSelect) {
8484
// Set the view and model value and update the angular template manually for the ajax/multiple select2.
8585
elm.bind("change", function () {
86+
if (scope.$$phase) return;
8687
scope.$apply(function () {
8788
controller.$setViewValue(elm.select2('data'));
8889
});

modules/directives/select2/test/select2Spec.js

Lines changed: 95 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
// a helper directive for injecting formatters and parsers
22
angular.module('ui.directives').directive('injectTransformers', [ function () {
3-
return {
4-
restrict: 'A',
5-
require: 'ngModel',
6-
priority: -1,
7-
link: function (scope, element, attr, ngModel) {
8-
var local = scope.$eval(attr.injectTransformers);
3+
return {
4+
restrict: 'A',
5+
require: 'ngModel',
6+
priority: -1,
7+
link: function (scope, element, attr, ngModel) {
8+
var local = scope.$eval(attr.injectTransformers);
99

10-
if (!angular.isObject(local) || !angular.isFunction(local.fromModel) || !angular.isFunction(local.fromElement)) {
11-
throw "The injectTransformers directive must be bound to an object with two functions (`fromModel` and `fromElement`)";
12-
}
10+
if (!angular.isObject(local) || !angular.isFunction(local.fromModel) || !angular.isFunction(local.fromElement)) {
11+
throw "The injectTransformers directive must be bound to an object with two functions (`fromModel` and `fromElement`)";
12+
}
1313

14-
ngModel.$parsers.push(local.fromElement);
15-
ngModel.$formatters.push(local.fromModel);
16-
}
17-
};
14+
ngModel.$parsers.push(local.fromElement);
15+
ngModel.$formatters.push(local.fromModel);
16+
}
17+
};
1818
}]);
1919

2020
/*global describe, beforeEach, module, inject, it, spyOn, expect, $ */
@@ -37,51 +37,50 @@ describe('uiSelect2', function () {
3737
};
3838

3939
scope.transformers = {
40-
fromModel: function (modelValue) {
41-
if (!modelValue) {
42-
return modelValue;
43-
}
44-
45-
if (angular.isArray(modelValue)) {
46-
return modelValue.map(function (val) {
47-
val.text += " - I've been formatted";
48-
return val;
49-
});
50-
}
51-
52-
if (angular.isObject(modelValue)) {
53-
modelValue.text += " - I've been formatted";
54-
return modelValue;
55-
}
40+
fromModel: function (modelValue) {
41+
if (!modelValue) {
42+
return modelValue;
43+
}
5644

57-
return modelValue + " - I've been formatted";
58-
},
59-
fromElement: function (elementValue) {
60-
var suffix = " - I've been formatted";
45+
if (angular.isArray(modelValue)) {
46+
return modelValue.map(function (val) {
47+
val.text += " - I've been formatted";
48+
return val;
49+
});
50+
}
6151

62-
if (!elementValue) {
63-
return elementValue;
64-
}
52+
if (angular.isObject(modelValue)) {
53+
modelValue.text += " - I've been formatted";
54+
return modelValue;
55+
}
6556

66-
if (angular.isArray(elementValue)) {
67-
return elementValue.map(function (val) {
68-
val.text += val.text.slice(0, val.text.indexOf(" - I've been formatted"));
69-
return val;
70-
});
71-
}
57+
return modelValue + " - I've been formatted";
58+
},
59+
fromElement: function (elementValue) {
60+
var suffix = " - I've been formatted";
7261

73-
if (angular.isObject(elementValue)) {
62+
if (!elementValue) {
63+
return elementValue;
64+
}
7465

75-
elementValue.text = elementValue.text.slice(0, elementValue.text.indexOf(suffix));
76-
return elementValue;
77-
}
66+
if (angular.isArray(elementValue)) {
67+
return elementValue.map(function (val) {
68+
val.text += val.text.slice(0, val.text.indexOf(" - I've been formatted"));
69+
return val;
70+
});
71+
}
7872

79-
if (elementValue) {
80-
return elementValue.slice(0, elementValue.indexOf(suffix));
81-
}
73+
if (angular.isObject(elementValue)) {
74+
elementValue.text = elementValue.text.slice(0, elementValue.text.indexOf(suffix));
75+
return elementValue;
76+
}
8277

83-
return undefined;
78+
if (elementValue) {
79+
return elementValue.slice(0, elementValue.indexOf(suffix));
8480
}
81+
82+
return undefined;
83+
}
8584
};
8685
}));
8786

@@ -110,21 +109,53 @@ describe('uiSelect2', function () {
110109
});
111110
});
112111
describe('when model is changed programmatically', function(){
113-
it('should set select2 to the value', function(){
114-
scope.foo = 'First';
115-
var element = compile('<select ui-select2 ng-model="foo"><option>First</option><option>Second</option></select>');
116-
expect(element.select2('val')).toBe('First');
117-
scope.$apply('foo = "Second"');
118-
expect(element.select2('val')).toBe('Second');
112+
describe('for single select', function(){
113+
it('should set select2 to the value', function(){
114+
scope.foo = 'First';
115+
var element = compile('<select ui-select2 ng-model="foo"><option>First</option><option>Second</option></select>');
116+
expect(element.select2('val')).toBe('First');
117+
scope.$apply('foo = "Second"');
118+
expect(element.select2('val')).toBe('Second');
119+
});
120+
it('should handle falsey values', function(){
121+
scope.foo = 'First';
122+
var element = compile('<select ui-select2="{allowClear:true}" ng-model="foo"><option>First</option><option>Second</option></select>');
123+
expect(element.select2('val')).toBe('First');
124+
scope.$apply('foo = false');
125+
expect(element.select2('val')).toBe(scope.foo);
126+
expect(element.select2('val')).toBe(false);
127+
scope.$apply('foo = null');
128+
expect(element.select2('val')).toBe(scope.foo);
129+
expect(element.select2('val')).toBe(null);
130+
scope.$apply('foo = undefined');
131+
expect(element.select2('val')).toBe(scope.foo);
132+
expect(element.select2('val')).toBe(undefined);
133+
});
119134
});
120-
it('should set select2 to the value for multiples', function(){
121-
scope.foo = 'First';
122-
var element = compile('<select ui-select2 multiple ng-model="foo"><option>First</option><option>Second</option><option>Third</option></select>');
123-
expect(element.select2('val')).toEqual(['First']);
124-
scope.$apply('foo = ["Second"]');
125-
expect(element.select2('val')).toEqual(['Second']);
126-
scope.$apply('foo = ["Second","Third"]');
127-
expect(element.select2('val')).toEqual(['Second','Third']);
135+
describe('for multiple select', function(){
136+
it('should set select2 to multiple value', function(){
137+
scope.foo = 'First';
138+
var element = compile('<select ui-select2="{allowClear:true}" multiple ng-model="foo"><option>First</option><option>Second</option><option>Third</option></select>');
139+
expect(element.select2('val')).toEqual(['First']);
140+
scope.$apply('foo = ["Second"]');
141+
expect(element.select2('val')).toEqual(['Second']);
142+
scope.$apply('foo = ["Second","Third"]');
143+
expect(element.select2('val')).toEqual(['Second','Third']);
144+
});
145+
it('should set select2 to the value for multiples', function(){
146+
scope.foo = 'First';
147+
var element = compile('<select ui-select2 multiple ng-model="foo"><option>First</option><option>Second</option><option>Third</option></select>');
148+
expect(element.select2('val')).toEqual(['First']);
149+
scope.$apply('foo = false');
150+
expect(element.select2('val')).toBe(scope.foo);
151+
expect(element.select2('val')).toBe(false);
152+
scope.$apply('foo = null');
153+
expect(element.select2('val')).toBe(scope.foo);
154+
expect(element.select2('val')).toBe(null);
155+
scope.$apply('foo = undefined');
156+
expect(element.select2('val')).toBe(scope.foo);
157+
expect(element.select2('val')).toBe(undefined);
158+
});
128159
});
129160
});
130161
it('should observe the disabled attribute', function () {

0 commit comments

Comments
 (0)