Procházet zdrojové kódy

make v-model checkbox updates replace the bound array (fix #3307)

Evan You před 10 roky
rodič
revize
c289a7e240

+ 6 - 5
src/directives/public/model/checkbox.js

@@ -31,15 +31,16 @@ export default {
     }
 
     this.listener = function () {
-      var model = self._watcher.value
+      var model = self._watcher.get()
       if (isArray(model)) {
         var val = self.getValue()
+        var i = indexOf(model, val)
         if (el.checked) {
-          if (indexOf(model, val) < 0) {
-            model.push(val)
+          if (i < 0) {
+            self.set(model.concat(val))
           }
-        } else {
-          model.$remove(val)
+        } else if (i > -1) {
+          self.set(model.slice(0, i).concat(model.slice(i + 1)))
         }
       } else {
         self.set(getBooleanValue())

+ 6 - 4
test/unit/specs/directives/public/model_spec.js

@@ -185,11 +185,13 @@ describe('v-model', function () {
     el.firstChild.click()
     expect(vm.list.length).toBe(2)
     expect(vm.list[1]).toBe(1)
-    vm.list = [vm.a]
     _.nextTick(function () {
-      expect(el.firstChild.checked).toBe(false)
-      expect(el.lastChild.checked).toBe(true)
-      done()
+      vm.list = [vm.a]
+      _.nextTick(function () {
+        expect(el.firstChild.checked).toBe(false)
+        expect(el.lastChild.checked).toBe(true)
+        done()
+      })
     })
   })