Răsfoiți Sursa

v-model: checkbox respect number param in array mode

Evan You 10 ani în urmă
părinte
comite
56dd2cf6ac
1 a modificat fișierele cu 11 adăugiri și 8 ștergeri
  1. 11 8
      src/directives/public/model/checkbox.js

+ 11 - 8
src/directives/public/model/checkbox.js

@@ -5,6 +5,15 @@ module.exports = {
   bind: function () {
     var self = this
     var el = this.el
+    var number = this.param('number') != null
+
+    this.getValue = function () {
+      return el.hasOwnProperty('_value')
+        ? el._value
+        : number
+          ? _.toNumber(el.value)
+          : el.value
+    }
 
     function getBooleanValue () {
       var val = el.checked
@@ -20,7 +29,7 @@ module.exports = {
     this.listener = function () {
       var model = self._watcher.value
       if (_.isArray(model)) {
-        var val = getValue(el)
+        var val = self.getValue()
         if (el.checked) {
           if (_.indexOf(model, val) < 0) {
             model.push(val)
@@ -42,7 +51,7 @@ module.exports = {
   update: function (value) {
     var el = this.el
     if (_.isArray(value)) {
-      el.checked = _.indexOf(value, getValue(el)) > -1
+      el.checked = _.indexOf(value, this.getValue()) > -1
     } else {
       if (el.hasOwnProperty('_trueValue')) {
         el.checked = _.looseEqual(value, el._trueValue)
@@ -52,9 +61,3 @@ module.exports = {
     }
   }
 }
-
-function getValue (el) {
-  return el.hasOwnProperty('_value')
-    ? el._value
-    : el.value
-}