github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/vue-1.0.24/src/directives/public/model/checkbox.js (about) 1 import { 2 toNumber, 3 isArray, 4 indexOf, 5 looseEqual 6 } from '../../../util/index' 7 8 export default { 9 10 bind () { 11 var self = this 12 var el = this.el 13 14 this.getValue = function () { 15 return el.hasOwnProperty('_value') 16 ? el._value 17 : self.params.number 18 ? toNumber(el.value) 19 : el.value 20 } 21 22 function getBooleanValue () { 23 var val = el.checked 24 if (val && el.hasOwnProperty('_trueValue')) { 25 return el._trueValue 26 } 27 if (!val && el.hasOwnProperty('_falseValue')) { 28 return el._falseValue 29 } 30 return val 31 } 32 33 this.listener = function () { 34 var model = self._watcher.value 35 if (isArray(model)) { 36 var val = self.getValue() 37 if (el.checked) { 38 if (indexOf(model, val) < 0) { 39 model.push(val) 40 } 41 } else { 42 model.$remove(val) 43 } 44 } else { 45 self.set(getBooleanValue()) 46 } 47 } 48 49 this.on('change', this.listener) 50 if (el.hasAttribute('checked')) { 51 this.afterBind = this.listener 52 } 53 }, 54 55 update (value) { 56 var el = this.el 57 if (isArray(value)) { 58 el.checked = indexOf(value, this.getValue()) > -1 59 } else { 60 if (el.hasOwnProperty('_trueValue')) { 61 el.checked = looseEqual(value, el._trueValue) 62 } else { 63 el.checked = !!value 64 } 65 } 66 } 67 }