Skip to content

Commit 7091255

Browse files
committed
v-model: checkbox respect number param in array mode
1 parent bf3ebcc commit 7091255

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/directives/model/checkbox.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@ module.exports = {
77
var el = this.el
88
var trueExp = this.param('true-exp')
99
var falseExp = this.param('false-exp')
10+
var number = this.param('number') != null
1011
var scope = this._scope || this.vm
1112

1213
if (process.env.NODE_ENV !== 'production' && (trueExp || falseExp)) {
1314
_.deprecation.MODEL_EXP(this.expression)
1415
}
1516

16-
this._matchValue = function (value) {
17+
this.getValue = function () {
18+
return el.hasOwnProperty('_value')
19+
? el._value
20+
: number
21+
? _.toNumber(el.value)
22+
: el.value
23+
}
24+
25+
this.matchValue = function (value) {
1726
if (el.hasOwnProperty('_trueValue')) {
1827
return _.looseEqual(value, el._trueValue)
1928
} else if (trueExp !== null) {
@@ -43,7 +52,7 @@ module.exports = {
4352
this.listener = function () {
4453
var model = self._watcher.value
4554
if (_.isArray(model)) {
46-
var val = getValue(el)
55+
var val = self.getValue()
4756
if (el.checked) {
4857
if (_.indexOf(model, val) < 0) {
4958
model.push(val)
@@ -65,15 +74,9 @@ module.exports = {
6574
update: function (value) {
6675
var el = this.el
6776
if (_.isArray(value)) {
68-
el.checked = _.indexOf(value, getValue(el)) > -1
77+
el.checked = _.indexOf(value, this.getValue()) > -1
6978
} else {
70-
el.checked = this._matchValue(value)
79+
el.checked = this.matchValue(value)
7180
}
7281
}
7382
}
74-
75-
function getValue (el) {
76-
return el.hasOwnProperty('_value')
77-
? el._value
78-
: el.value
79-
}

0 commit comments

Comments
 (0)