Skip to content

Commit feab94f

Browse files
committed
use Directive.on
1 parent 84a2c70 commit feab94f

File tree

7 files changed

+85
-96
lines changed

7 files changed

+85
-96
lines changed

src/directive.js

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function Directive (name, el, vm, descriptor, def, host) {
3737
this._host = host
3838
this._locked = false
3939
this._bound = false
40+
this._listeners = null
4041
// init
4142
this._bind(def)
4243
}
@@ -171,23 +172,6 @@ p._checkParam = function (name) {
171172
return param
172173
}
173174

174-
/**
175-
* Teardown the watcher and call unbind.
176-
*/
177-
178-
p._teardown = function () {
179-
if (this._bound) {
180-
this._bound = false
181-
if (this.unbind) {
182-
this.unbind()
183-
}
184-
if (this._watcher) {
185-
this._watcher.teardown()
186-
}
187-
this.vm = this.el = this._watcher = null
188-
}
189-
}
190-
191175
/**
192176
* Set the corresponding value with the setter.
193177
* This should only be used in two-way directives
@@ -227,4 +211,43 @@ p._withLock = function (fn) {
227211
})
228212
}
229213

214+
/**
215+
* Convenience method that attaches a DOM event listener
216+
* to the directive element and autometically tears it down
217+
* during unbind.
218+
*
219+
* @param {String} event
220+
* @param {Function} handler
221+
*/
222+
223+
p.on = function (event, handler) {
224+
_.on(this.el, event, handler)
225+
;(this._listeners || (this._listeners = []))
226+
.push([event, handler])
227+
}
228+
229+
/**
230+
* Teardown the watcher and call unbind.
231+
*/
232+
233+
p._teardown = function () {
234+
if (this._bound) {
235+
this._bound = false
236+
if (this.unbind) {
237+
this.unbind()
238+
}
239+
if (this._watcher) {
240+
this._watcher.teardown()
241+
}
242+
var listeners = this._listeners
243+
if (listeners) {
244+
for (var i = 0; i < listeners.length; i++) {
245+
_.off(this.el, listeners[i][0], listeners[i][1])
246+
}
247+
}
248+
this.vm = this.el =
249+
this._watcher = this._listeners = null
250+
}
251+
}
252+
230253
module.exports = Directive

src/directives/model/checkbox.js

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
var _ = require('../../util')
2-
31
module.exports = {
42

53
bind: function () {
@@ -8,6 +6,14 @@ module.exports = {
86
var trueExp = this._checkParam('true-exp')
97
var falseExp = this._checkParam('false-exp')
108

9+
this._matchValue = function (value) {
10+
var trueValue = true
11+
if (trueExp !== null) {
12+
trueValue = self.vm.$eval(trueExp)
13+
}
14+
return trueValue === value
15+
}
16+
1117
function getValue () {
1218
var val = el.checked
1319
if (val && trueExp !== null) {
@@ -18,31 +24,17 @@ module.exports = {
1824
}
1925
return val
2026
}
21-
this._getValue = getValue
2227

23-
function matchValue (value) {
24-
var trueValue = true
25-
if (trueExp !== null) {
26-
trueValue = self.vm.$eval(trueExp)
27-
}
28-
return trueValue === value
29-
}
30-
this._matchValue = matchValue
31-
32-
this.listener = function () {
28+
this.on('change', function () {
3329
self.set(getValue())
34-
}
35-
_.on(el, 'change', this.listener)
30+
})
31+
3632
if (el.checked) {
3733
this._initValue = getValue()
3834
}
3935
},
4036

4137
update: function (value) {
4238
this.el.checked = this._matchValue(value)
43-
},
44-
45-
unbind: function () {
46-
_.off(this.el, 'change', this.listener)
4739
}
4840
}

src/directives/model/radio.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = {
77
var el = this.el
88
var number = this._checkParam('number') != null
99
var expression = this._checkParam('exp')
10-
function getValue () {
10+
11+
this.getValue = function () {
1112
var val = el.value
1213
if (number) {
1314
val = _.toNumber(val)
@@ -16,23 +17,19 @@ module.exports = {
1617
}
1718
return val
1819
}
19-
this._getValue = getValue
20-
this.listener = function () {
21-
self.set(getValue())
22-
}
23-
_.on(el, 'change', this.listener)
20+
21+
this.on('change', function () {
22+
self.set(self.getValue())
23+
})
24+
2425
if (el.checked) {
25-
this._initValue = getValue()
26+
this._initValue = this.getValue()
2627
}
2728
},
2829

2930
update: function (value) {
3031
/* eslint-disable eqeqeq */
31-
this.el.checked = value == this._getValue()
32+
this.el.checked = value == this.getValue()
3233
/* eslint-enable eqeqeq */
33-
},
34-
35-
unbind: function () {
36-
_.off(this.el, 'change', this.listener)
3734
}
3835
}

src/directives/model/select.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = {
2020
}
2121
this.number = this._checkParam('number') != null
2222
this.multiple = el.hasAttribute('multiple')
23-
this.listener = function () {
23+
this.on('change', function () {
2424
var value = self.multiple
2525
? getMultiValue(el)
2626
: el.value
@@ -30,8 +30,7 @@ module.exports = {
3030
: _.toNumber(value)
3131
: value
3232
self.set(value)
33-
}
34-
_.on(el, 'change', this.listener)
33+
})
3534
checkInitialValue.call(this)
3635
// All major browsers except Firefox resets
3736
// selectedIndex with value -1 to 0 when the element
@@ -64,13 +63,11 @@ module.exports = {
6463
},
6564

6665
unbind: function () {
67-
_.off(this.el, 'change', this.listener)
6866
this.vm.$off('hook:attached', this.forceUpdate)
6967
if (this.optionWatcher) {
7068
this.optionWatcher.teardown()
7169
}
7270
}
73-
7471
}
7572

7673
/**

src/directives/model/text.js

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,29 @@ module.exports = {
2424
// suggestions... (see Discussion/#162)
2525
var composing = false
2626
if (!_.isAndroid && !isRange) {
27-
this.onComposeStart = function () {
27+
this.on('compositionstart', function () {
2828
composing = true
29-
}
30-
this.onComposeEnd = function () {
29+
})
30+
this.on('compositionend', function () {
3131
composing = false
3232
// in IE11 the "compositionend" event fires AFTER
3333
// the "input" event, so the input handler is blocked
3434
// at the end... have to call it here.
3535
self.listener()
36-
}
37-
_.on(el, 'compositionstart', this.onComposeStart)
38-
_.on(el, 'compositionend', this.onComposeEnd)
36+
})
3937
}
4038

4139
// prevent messing with the input when user is typing,
4240
// and force update on blur.
4341
this.focused = false
4442
if (!isRange) {
45-
this.onFocus = function () {
43+
this.on('focus', function () {
4644
self.focused = true
47-
}
48-
this.onBlur = function () {
45+
})
46+
this.on('blur', function () {
4947
self.focused = false
5048
self.listener()
51-
}
52-
_.on(el, 'focus', this.onFocus)
53-
_.on(el, 'blur', this.onBlur)
49+
})
5450
}
5551

5652
// Now attach the main listener
@@ -63,7 +59,9 @@ module.exports = {
6359
// force update here, because the watcher may not
6460
// run when the value is the same.
6561
_.nextTick(function () {
66-
self.update(self._watcher.value)
62+
if (self._bound) {
63+
self.update(self._watcher.value)
64+
}
6765
})
6866
}
6967
if (debounce) {
@@ -87,24 +85,22 @@ module.exports = {
8785
jQuery(el).on('input', this.listener)
8886
}
8987
} else {
90-
_.on(el, 'change', this.listener)
88+
this.on('change', this.listener)
9189
if (!lazy) {
92-
_.on(el, 'input', this.listener)
90+
this.on('input', this.listener)
9391
}
9492
}
9593

9694
// IE9 doesn't fire input event on backspace/del/cut
9795
if (!lazy && _.isIE9) {
98-
this.onCut = function () {
96+
this.on('cut', function () {
9997
_.nextTick(self.listener)
100-
}
101-
this.onDel = function (e) {
98+
})
99+
this.on('keyup', function (e) {
102100
if (e.keyCode === 46 || e.keyCode === 8) {
103101
self.listener()
104102
}
105-
}
106-
_.on(el, 'cut', this.onCut)
107-
_.on(el, 'keyup', this.onDel)
103+
})
108104
}
109105

110106
// set initial value if present
@@ -129,21 +125,6 @@ module.exports = {
129125
if (this.hasjQuery) {
130126
jQuery(el).off('change', this.listener)
131127
jQuery(el).off('input', this.listener)
132-
} else {
133-
_.off(el, 'change', this.listener)
134-
_.off(el, 'input', this.listener)
135-
}
136-
if (this.onComposeStart) {
137-
_.off(el, 'compositionstart', this.onComposeStart)
138-
_.off(el, 'compositionend', this.onComposeEnd)
139-
}
140-
if (this.onCut) {
141-
_.off(el, 'cut', this.onCut)
142-
_.off(el, 'keyup', this.onDel)
143-
}
144-
if (this.onFocus) {
145-
_.off(el, 'focus', this.onFocus)
146-
_.off(el, 'blur', this.onBlur)
147128
}
148129
}
149130
}

src/directives/on.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
this.iframeBind = function () {
1616
_.on(self.el.contentWindow, self.arg, self.handler)
1717
}
18-
_.on(this.el, 'load', this.iframeBind)
18+
this.on('load', this.iframeBind)
1919
}
2020
},
2121

@@ -55,6 +55,5 @@ module.exports = {
5555

5656
unbind: function () {
5757
this.reset()
58-
_.off(this.el, 'load', this.iframeBind)
5958
}
6059
}

test/unit/specs/directives/model_spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ if (_.inBrowser) {
6464
expect(el.childNodes[0].checked).toBe(true)
6565
expect(el.childNodes[1].checked).toBe(false)
6666
expect(vm.test).toBe(1)
67-
vm._directives[1].unbind()
67+
vm._directives[1]._teardown()
6868
el.childNodes[1].click()
6969
expect(vm.test).toBe(1)
7070
done()
@@ -124,7 +124,7 @@ if (_.inBrowser) {
124124
el.firstChild.click()
125125
expect(el.firstChild.checked).toBe(true)
126126
expect(vm.test).toBe(true)
127-
vm._directives[0].unbind()
127+
vm._directives[0]._teardown()
128128
el.firstChild.click()
129129
expect(el.firstChild.checked).toBe(false)
130130
expect(vm.test).toBe(true)
@@ -471,7 +471,7 @@ if (_.inBrowser) {
471471
el.firstChild.value = 'c'
472472
trigger(el.firstChild, 'input')
473473
expect(vm.test).toBe('c')
474-
vm._directives[0].unbind()
474+
vm._directives[0]._teardown()
475475
el.firstChild.value = 'd'
476476
trigger(el.firstChild, 'input')
477477
expect(vm.test).toBe('c')
@@ -614,7 +614,7 @@ if (_.inBrowser) {
614614
})
615615
expect(vm.test).toBe('a')
616616
// teardown
617-
vm._directives[0].unbind()
617+
vm._directives[0]._teardown()
618618
input.value = 'bbb'
619619
trigger(input, 'keyup', function (e) {
620620
e.keyCode = 8
@@ -720,7 +720,7 @@ if (_.inBrowser) {
720720
el.firstChild.value = 'c'
721721
jQuery(el.firstChild).trigger('change')
722722
expect(vm.test).toBe('c')
723-
vm._directives[0].unbind()
723+
vm._directives[0]._teardown()
724724
el.firstChild.value = 'd'
725725
jQuery(el.firstChild).trigger('change')
726726
expect(vm.test).toBe('c')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy