@@ -5,6 +5,7 @@ module.exports = {
5
5
bind : function ( ) {
6
6
var self = this
7
7
var el = this . el
8
+ var isRange = el . type === 'range'
8
9
9
10
// check params
10
11
// - lazy: update model on "change" instead of "input"
@@ -22,7 +23,7 @@ module.exports = {
22
23
// Chinese, but instead triggers them for spelling
23
24
// suggestions... (see Discussion/#162)
24
25
var composing = false
25
- if ( ! _ . isAndroid ) {
26
+ if ( ! _ . isAndroid && ! isRange ) {
26
27
this . onComposeStart = function ( ) {
27
28
composing = true
28
29
}
@@ -37,63 +38,38 @@ module.exports = {
37
38
_ . on ( el , 'compositionend' , this . onComposeEnd )
38
39
}
39
40
40
- function syncToModel ( ) {
41
- var val = number
42
- ? _ . toNumber ( el . value )
43
- : el . value
44
- self . set ( val )
45
- }
46
-
47
- // if the directive has filters, we need to
48
- // record cursor position and restore it after updating
49
- // the input with the filtered value.
50
- // also force update for type="range" inputs to enable
51
- // "lock in range" (see #506)
52
- if ( this . hasRead || el . type === 'range' ) {
53
- this . listener = function ( ) {
54
- if ( composing ) return
55
- var charsOffset
56
- // some HTML5 input types throw error here
57
- try {
58
- // record how many chars from the end of input
59
- // the cursor was at
60
- charsOffset = el . value . length - el . selectionStart
61
- } catch ( e ) { }
62
- // Fix IE10/11 infinite update cycle
63
- // https://github.com/yyx990803/vue/issues/592
64
- /* istanbul ignore if */
65
- if ( charsOffset < 0 ) {
66
- return
67
- }
68
- syncToModel ( )
69
- _ . nextTick ( function ( ) {
70
- // force a value update, because in
71
- // certain cases the write filters output the
72
- // same result for different input values, and
73
- // the Observer set events won't be triggered.
74
- var newVal = self . _watcher . value
75
- self . update ( newVal )
76
- if ( charsOffset != null ) {
77
- var cursorPos =
78
- _ . toString ( newVal ) . length - charsOffset
79
- el . setSelectionRange ( cursorPos , cursorPos )
80
- }
81
- } )
41
+ // prevent messing with the input when user is typing,
42
+ // and force update on blur.
43
+ this . focused = false
44
+ if ( ! isRange ) {
45
+ this . onFocus = function ( ) {
46
+ self . focused = true
82
47
}
83
- } else {
84
- this . listener = function ( ) {
85
- if ( composing ) return
86
- syncToModel ( )
48
+ this . onBlur = function ( ) {
49
+ self . focused = false
50
+ self . listener ( )
87
51
}
52
+ _ . on ( el , 'focus' , this . onFocus )
53
+ _ . on ( el , 'blur' , this . onBlur )
88
54
}
89
55
56
+ // Now attach the main listener
57
+ this . listener = function ( ) {
58
+ if ( composing ) return
59
+ var val = number || isRange
60
+ ? _ . toNumber ( el . value )
61
+ : el . value
62
+ self . set ( val )
63
+ // force update here, because the watcher may not
64
+ // run when the value is the same.
65
+ _ . nextTick ( function ( ) {
66
+ self . update ( self . _watcher . value )
67
+ } )
68
+ }
90
69
if ( debounce ) {
91
70
this . listener = _ . debounce ( this . listener , debounce )
92
71
}
93
72
94
- // Now attach the main listener
95
-
96
- this . event = lazy ? 'change' : 'input'
97
73
// Support jQuery events, since jQuery.trigger() doesn't
98
74
// trigger native events in some cases and some plugins
99
75
// rely on $.trigger()
@@ -106,9 +82,15 @@ module.exports = {
106
82
// jQuery variable in tests.
107
83
this . hasjQuery = typeof jQuery === 'function'
108
84
if ( this . hasjQuery ) {
109
- jQuery ( el ) . on ( this . event , this . listener )
85
+ jQuery ( el ) . on ( 'change' , this . listener )
86
+ if ( ! lazy ) {
87
+ jQuery ( el ) . on ( 'input' , this . listener )
88
+ }
110
89
} else {
111
- _ . on ( el , this . event , this . listener )
90
+ _ . on ( el , 'change' , this . listener )
91
+ if ( ! lazy ) {
92
+ _ . on ( el , 'input' , this . listener )
93
+ }
112
94
}
113
95
114
96
// IE9 doesn't fire input event on backspace/del/cut
@@ -137,15 +119,19 @@ module.exports = {
137
119
} ,
138
120
139
121
update : function ( value ) {
140
- this . el . value = _ . toString ( value )
122
+ if ( ! this . focused ) {
123
+ this . el . value = _ . toString ( value )
124
+ }
141
125
} ,
142
126
143
127
unbind : function ( ) {
144
128
var el = this . el
145
129
if ( this . hasjQuery ) {
146
- jQuery ( el ) . off ( this . event , this . listener )
130
+ jQuery ( el ) . off ( 'change' , this . listener )
131
+ jQuery ( el ) . off ( 'input' , this . listener )
147
132
} else {
148
- _ . off ( el , this . event , this . listener )
133
+ _ . off ( el , 'change' , this . listener )
134
+ _ . off ( el , 'input' , this . listener )
149
135
}
150
136
if ( this . onComposeStart ) {
151
137
_ . off ( el , 'compositionstart' , this . onComposeStart )
@@ -155,5 +141,9 @@ module.exports = {
155
141
_ . off ( el , 'cut' , this . onCut )
156
142
_ . off ( el , 'keyup' , this . onDel )
157
143
}
144
+ if ( this . onFocus ) {
145
+ _ . off ( el , 'focus' , this . onFocus )
146
+ _ . off ( el , 'blur' , this . onBlur )
147
+ }
158
148
}
159
149
}
0 commit comments