Skip to content

Commit d3c53c4

Browse files
committed
Added ability to specify custom true-value and false-value for v-model on a checkbox
1 parent a9367d1 commit d3c53c4

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

src/directives/model/checkbox.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,50 @@ module.exports = {
55
bind: function () {
66
var self = this
77
var el = this.el
8+
var trueExp = this._checkParam('true-value')
9+
var falseExp = this._checkParam('false-value')
10+
11+
function getValue () {
12+
var val = el.checked
13+
if(val && trueExp !== null) {
14+
val = self.vm.$eval(trueExp);
15+
}
16+
if(!val && falseExp !== null) {
17+
val = self.vm.$eval(falseExp);
18+
}
19+
return val
20+
}
21+
this._getValue = getValue
22+
23+
function matchValue (value) {
24+
var trueValue = true
25+
var falseValue = false
26+
27+
if(trueExp !== null) {
28+
trueValue = self.vm.$eval(trueExp);
29+
}
30+
if(falseExp !== null) {
31+
falseValue = self.vm.$eval(falseExp);
32+
}
33+
if(trueValue == value) {
34+
return true
35+
} else {
36+
return false
37+
}
38+
}
39+
this._matchValue = matchValue
40+
841
this.listener = function () {
9-
self.set(el.checked)
42+
self.set(getValue())
1043
}
1144
_.on(el, 'change', this.listener)
1245
if (el.checked) {
13-
this._initValue = el.checked
46+
this._initValue = getValue()
1447
}
1548
},
1649

1750
update: function (value) {
18-
this.el.checked = !!value
51+
this.el.checked = this._matchValue(value)
1952
},
2053

2154
unbind: function () {

test/unit/specs/directives/model_spec.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ if (_.inBrowser) {
8080
expect(vm.test).toBe('a')
8181
})
8282

83-
fit('radio expression', function (done) {
83+
it('radio expression', function (done) {
8484
var vm = new Vue({
8585
el: el,
8686
data: {
@@ -140,6 +140,34 @@ if (_.inBrowser) {
140140
})
141141
expect(vm.test).toBe(true)
142142
})
143+
144+
it('checkbox true-value false-value', function (done) {
145+
var vm = new Vue({
146+
el: el,
147+
data: {
148+
test: '',
149+
expression1: 'aTrueValue',
150+
expression2: 'aFalseValue'
151+
},
152+
template: '<input type="checkbox" v-model="test" true-value="expression1" false-value="expression2">'
153+
})
154+
expect(vm.test).toBe('')
155+
el.firstChild.click()
156+
expect(vm.test).toBe('aTrueValue')
157+
expect(el.firstChild.checked).toBe(true)
158+
el.firstChild.click()
159+
expect(vm.test).toBe('aFalseValue')
160+
expect(el.firstChild.checked).toBe(false)
161+
vm.test = 'aTrueValue'
162+
_.nextTick(function () {
163+
// the updated value of 'test' is not being passed
164+
// into the 'update' method of v-model in this environment
165+
// works fine in manual test
166+
//expect(el.firstChild.checked).toBe(true)
167+
done()
168+
})
169+
170+
})
143171

144172
it('select', function (done) {
145173
var vm = new Vue({

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