Skip to content

Commit 7bed9a6

Browse files
committed
also support jquery input events for v-model, based on vuejs#745
1 parent bd47f1c commit 7bed9a6

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/directives/model/default.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var _ = require('../../util')
2-
var hasjQuery = typeof jQuery === 'function'
32

43
module.exports = {
54

@@ -85,12 +84,21 @@ module.exports = {
8584
this.listener = _.debounce(this.listener, debounce)
8685
}
8786
this.event = lazy ? 'change' : 'input'
88-
_.on(el, this.event, this.listener)
89-
90-
// support jQuery change event, jQuery.trigger() doesn't
91-
// trigger native change event in some cases
92-
if (hasjQuery) {
93-
jQuery(el).on('change', this.listener)
87+
// Support jQuery events, since jQuery.trigger() doesn't
88+
// trigger native events in some cases and some plugins
89+
// rely on $.trigger()
90+
//
91+
// We want to make sure if a listener is attached using
92+
// jQuery, it is also removed with jQuery, that's why
93+
// we do the check for each directive instance and
94+
// store that check result on itself. This also allows
95+
// easier test coverage control by unsetting the global
96+
// jQuery variable in tests.
97+
this.hasjQuery = typeof jQuery === 'function'
98+
if (this.hasjQuery) {
99+
jQuery(el).on(this.event, this.listener)
100+
} else {
101+
_.on(el, this.event, this.listener)
94102
}
95103

96104
// IE9 doesn't fire input event on backspace/del/cut
@@ -124,9 +132,10 @@ module.exports = {
124132

125133
unbind: function () {
126134
var el = this.el
127-
_.off(el, this.event, this.listener)
128-
if (hasjQuery) {
129-
jQuery(el).off('change', this.listener)
135+
if (this.hasjQuery) {
136+
jQuery(el).off(this.event, this.listener)
137+
} else {
138+
_.off(el, this.event, this.listener)
130139
}
131140
_.off(el,'compositionstart', this.cpLock)
132141
_.off(el,'compositionend', this.cpUnlock)

test/unit/specs/directives/model_spec.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
var _ = require('../../../../src/util')
22
var Vue = require('../../../../src/vue')
33

4+
// unset jQuery to bypass jQuery check for normal test cases
5+
jQuery = null
6+
47
/**
58
* Mock event helper
69
*/
@@ -570,12 +573,14 @@ if (_.inBrowser) {
570573
})
571574

572575
it('support jQuery change event', function (done) {
576+
// restore jQuery
577+
jQuery = $
573578
var vm = new Vue({
574579
el: el,
575580
data: {
576581
test: 'b'
577582
},
578-
template: '<input v-model="test">'
583+
template: '<input v-model="test" lazy>'
579584
})
580585
expect(el.firstChild.value).toBe('b')
581586
vm.test = 'a'
@@ -588,6 +593,8 @@ if (_.inBrowser) {
588593
el.firstChild.value = 'd'
589594
jQuery(el.firstChild).trigger('change')
590595
expect(vm.test).toBe('c')
596+
// unset jQuery
597+
jQuery = null
591598
done()
592599
})
593600
})

0 commit comments

Comments
 (0)