1
1
// a helper directive for injecting formatters and parsers
2
2
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 ) ;
9
9
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
+ }
13
13
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
+ } ;
18
18
} ] ) ;
19
19
20
20
/*global describe, beforeEach, module, inject, it, spyOn, expect, $ */
@@ -37,51 +37,50 @@ describe('uiSelect2', function () {
37
37
} ;
38
38
39
39
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
+ }
56
44
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
+ }
61
51
62
- if ( ! elementValue ) {
63
- return elementValue ;
64
- }
52
+ if ( angular . isObject ( modelValue ) ) {
53
+ modelValue . text += " - I've been formatted" ;
54
+ return modelValue ;
55
+ }
65
56
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" ;
72
61
73
- if ( angular . isObject ( elementValue ) ) {
62
+ if ( ! elementValue ) {
63
+ return elementValue ;
64
+ }
74
65
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
+ }
78
72
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
+ }
82
77
83
- return undefined ;
78
+ if ( elementValue ) {
79
+ return elementValue . slice ( 0 , elementValue . indexOf ( suffix ) ) ;
84
80
}
81
+
82
+ return undefined ;
83
+ }
85
84
} ;
86
85
} ) ) ;
87
86
@@ -110,21 +109,53 @@ describe('uiSelect2', function () {
110
109
} ) ;
111
110
} ) ;
112
111
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
+ } ) ;
119
134
} ) ;
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
+ } ) ;
128
159
} ) ;
129
160
} ) ;
130
161
it ( 'should observe the disabled attribute' , function ( ) {
0 commit comments