Просмотр исходного кода

fix v-repeat value sync for null and boolean values (fix #1108)

Evan You 11 лет назад
Родитель
Сommit
f80276395d
2 измененных файлов с 25 добавлено и 9 удалено
  1. 17 5
      src/directives/repeat.js
  2. 8 4
      test/unit/specs/directives/repeat_spec.js

+ 17 - 5
src/directives/repeat.js

@@ -386,12 +386,8 @@ module.exports = {
       this.cacheVm(raw, vm, index, this.converted ? meta.$key : null)
     }
     // sync back changes for two-way bindings of primitive values
-    var type = typeof raw
     var dir = this
-    if (
-      this.rawType === 'object' &&
-      (type === 'string' || type === 'number')
-    ) {
+    if (this.rawType === 'object' && isPrimitive(raw)) {
       vm.$watch(alias || '$value', function (val) {
         if (dir.filters) {
           process.env.NODE_ENV !== 'production' && _.warn(
@@ -740,3 +736,19 @@ function toRefObject (vms) {
   }
   return ref
 }
+
+/**
+ * Check if a value is a primitive one:
+ * String, Number, Boolean, null or undefined.
+ *
+ * @param {*} value
+ * @return {Boolean}
+ */
+
+function isPrimitive (value) {
+  var type = typeof value
+  return value == null ||
+    type === 'string' ||
+    type === 'number' ||
+    type === 'boolean'
+}

+ 8 - 4
test/unit/specs/directives/repeat_spec.js

@@ -669,19 +669,23 @@ if (_.inBrowser) {
           '<div v-repeat="obj">{{$value}}</div>' +
           '<div v-repeat="val:vals">{{val}}</div>',
         data: {
-          items: ['a', 'b'],
+          items: ['a', true],
           obj: { foo: 'a', bar: 'b' },
-          vals: [1, 2]
+          vals: [1, null]
         }
       })
       vm.$children[0].$value = 'c'
+      vm.$children[1].$value = 'd'
       var key = vm.$children[2].$key
-      vm.$children[2].$value = 'd'
+      vm.$children[2].$value = 'e'
       vm.$children[4].val = 3
+      vm.$children[5].val = 4
       _.nextTick(function () {
         expect(vm.items[0]).toBe('c')
-        expect(vm.obj[key]).toBe('d')
+        expect(vm.items[1]).toBe('d')
+        expect(vm.obj[key]).toBe('e')
         expect(vm.vals[0]).toBe(3)
+        expect(vm.vals[1]).toBe(4)
         done()
       })
     })