@@ -14,21 +14,26 @@ module.exports = {
14
14
}
15
15
16
16
this . number = this . param ( 'number' ) != null
17
- this . multiple = el . hasAttribute ( 'multiple' )
17
+ var multiple = this . multiple = el . hasAttribute ( 'multiple' )
18
18
19
19
// attach listener
20
- this . on ( 'change' , function ( ) {
21
- var value = getValue ( el , self . multiple )
20
+ this . listener = function ( ) {
21
+ var value = getValue ( el , multiple )
22
22
value = self . number
23
23
? _ . isArray ( value )
24
24
? value . map ( _ . toNumber )
25
25
: _ . toNumber ( value )
26
26
: value
27
27
self . set ( value )
28
- } )
28
+ }
29
+ this . on ( 'change' , this . listener )
29
30
30
- // check initial value (inline selected attribute)
31
- checkInitialValue . call ( this )
31
+ // if has initial value, set afterBind
32
+ var initValue = getValue ( el , multiple , true )
33
+ if ( ( multiple && initValue . length ) ||
34
+ ( ! multiple && initValue !== null ) ) {
35
+ this . afterBind = this . listener
36
+ }
32
37
33
38
// All major browsers except Firefox resets
34
39
// selectedIndex with value -1 to 0 when the element
@@ -63,44 +68,24 @@ module.exports = {
63
68
}
64
69
}
65
70
66
- /**
67
- * Check the initial value for selected options.
68
- */
69
-
70
- function checkInitialValue ( ) {
71
- var initValue
72
- var options = this . el . options
73
- for ( var i = 0 , l = options . length ; i < l ; i ++ ) {
74
- if ( options [ i ] . hasAttribute ( 'selected' ) ) {
75
- if ( this . multiple ) {
76
- ( initValue || ( initValue = [ ] ) )
77
- . push ( options [ i ] . value )
78
- } else {
79
- initValue = options [ i ] . value
80
- }
81
- }
82
- }
83
- if ( typeof initValue !== 'undefined' ) {
84
- this . _initValue = this . number
85
- ? _ . toNumber ( initValue )
86
- : initValue
87
- }
88
- }
89
-
90
71
/**
91
72
* Get select value
92
73
*
93
74
* @param {SelectElement } el
94
75
* @param {Boolean } multi
76
+ * @param {Boolean } init
95
77
* @return {Array|* }
96
78
*/
97
79
98
- function getValue ( el , multi ) {
80
+ function getValue ( el , multi , init ) {
99
81
var res = multi ? [ ] : null
100
- var op , val
82
+ var op , val , selected
101
83
for ( var i = 0 , l = el . options . length ; i < l ; i ++ ) {
102
84
op = el . options [ i ]
103
- if ( op . selected ) {
85
+ selected = init
86
+ ? op . hasAttribute ( 'selected' )
87
+ : op . selected
88
+ if ( selected ) {
104
89
val = op . hasOwnProperty ( '_value' )
105
90
? op . _value
106
91
: op . value
0 commit comments