Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/directives/model/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,41 @@ module.exports = {
bind: function () {
var self = this
var el = this.el
var trueExp = this._checkParam('true-exp')
var falseExp = this._checkParam('false-exp')

function getValue () {
var val = el.checked
if (val && trueExp !== null) {
val = self.vm.$eval(trueExp)
}
if (!val && falseExp !== null) {
val = self.vm.$eval(falseExp)
}
return val
}
this._getValue = getValue

function matchValue (value) {
var trueValue = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since el.checked can be only true or false, we just need to check if value equals trueValue here - in all other cases we just return false. This could make matchValue much simpler.

if (trueExp !== null) {
trueValue = self.vm.$eval(trueExp)
}
return trueValue === value
}
this._matchValue = matchValue

this.listener = function () {
self.set(el.checked)
self.set(getValue())
}
_.on(el, 'change', this.listener)
if (el.checked) {
this._initValue = el.checked
this._initValue = getValue()
}
},

update: function (value) {
this.el.checked = !!value
this.el.checked = this._matchValue(value)
},

unbind: function () {
Expand Down
14 changes: 10 additions & 4 deletions src/directives/model/radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ module.exports = {
var self = this
var el = this.el
var number = this._checkParam('number') != null
var expression = this._checkParam('exp')
function getValue () {
return number
? _.toNumber(el.value)
: el.value
var val = el.value
if (number) {
val = _.toNumber(val)
} else if (expression !== null) {
val = self.vm.$eval(expression)
}
return val
}
this._getValue = getValue
this.listener = function () {
self.set(getValue())
}
Expand All @@ -22,7 +28,7 @@ module.exports = {

update: function (value) {
/* eslint-disable eqeqeq */
this.el.checked = value == this.el.value
this.el.checked = value == this._getValue()
/* eslint-enable eqeqeq */
},

Expand Down
56 changes: 56 additions & 0 deletions test/unit/specs/directives/model_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ if (_.inBrowser) {
expect(vm.test).toBe('a')
})

it('radio expression', function (done) {
var vm = new Vue({
el: el,
data: {
test: false,
test2: 'string1',
expression1: 'string1',
expression2: 'string2'
},
template:
'<input type="radio" value="1" v-model="test" name="test" exp="true">' +
'<input type="radio" value="0" v-model="test" name="test" exp="false">' +
'<input type="radio" value="1" v-model="test2" name="test2" exp="expression1">' +
'<input type="radio" value="0" v-model="test2" name="test2" exp="expression2">'
})
expect(el.childNodes[0].checked).toBe(false)
expect(el.childNodes[1].checked).toBe(true)
expect(el.childNodes[2].checked).toBe(true)
expect(el.childNodes[3].checked).toBe(false)
_.nextTick(function () {
el.childNodes[0].click()
expect(vm.test).toBe(true)
el.childNodes[3].click()
expect(vm.test2).toBe('string2')
done()
})
})

it('checkbox', function (done) {
var vm = new Vue({
el: el,
Expand Down Expand Up @@ -113,6 +141,34 @@ if (_.inBrowser) {
expect(vm.test).toBe(true)
})

it('checkbox expression', function (done) {
var vm = new Vue({
el: el,
data: {
test: '',
expression1: 'aTrueValue',
expression2: 'aFalseValue'
},
template: '<input type="checkbox" v-model="test" true-exp="expression1" false-exp="expression2">'
})
expect(vm.test).toBe('')
el.firstChild.click()
expect(vm.test).toBe('aTrueValue')
expect(el.firstChild.checked).toBe(true)
el.firstChild.click()
expect(vm.test).toBe('aFalseValue')
expect(el.firstChild.checked).toBe(false)
vm.test = 'aTrueValue'
_.nextTick(function () {
// the updated value of 'test' is not being passed
// into the 'update' method of v-model in this environment
// works fine in manual test
// expect(el.firstChild.checked).toBe(true)
done()
})

})

it('select', function (done) {
var vm = new Vue({
el: el,
Expand Down