Parcourir la source

fix Array linking converting already converted objects, minor fix for v-repeat

Evan You il y a 12 ans
Parent
commit
f1cd944071
2 fichiers modifiés avec 14 ajouts et 32 suppressions
  1. 8 30
      src/directives/repeat.js
  2. 6 2
      src/observer.js

+ 8 - 30
src/directives/repeat.js

@@ -98,15 +98,11 @@ module.exports = {
                     self.vms[i].$index = i
                     self.vms[i].$index = i
                 }
                 }
             }
             }
-            if (method === 'push' || method === 'unshift' || method === 'splice') {
-                // recalculate dependency
-                self.changed()
-            }
         }
         }
 
 
     },
     },
 
 
-    update: function (collection, init) {
+    update: function (collection) {
 
 
         if (
         if (
             collection === this.collection ||
             collection === this.collection ||
@@ -127,7 +123,6 @@ module.exports = {
 
 
         // keep reference of old data and VMs
         // keep reference of old data and VMs
         // so we can reuse them if possible
         // so we can reuse them if possible
-        this.old = this.collection
         var oldVMs = this.oldVMs = this.vms
         var oldVMs = this.oldVMs = this.vms
 
 
         collection = this.collection = collection || []
         collection = this.collection = collection || []
@@ -147,7 +142,6 @@ module.exports = {
         // create new VMs and append to DOM
         // create new VMs and append to DOM
         if (collection.length) {
         if (collection.length) {
             collection.forEach(this.build, this)
             collection.forEach(this.build, this)
-            if (!init) this.changed()
         }
         }
 
 
         // listen for object changes and sync the repeater
         // listen for object changes and sync the repeater
@@ -158,7 +152,7 @@ module.exports = {
 
 
         // destroy unused old VMs
         // destroy unused old VMs
         if (oldVMs) destroyVMs(oldVMs)
         if (oldVMs) destroyVMs(oldVMs)
-        this.old = this.oldVMs = null
+        this.oldVMs = null
     },
     },
 
 
     addItems: function (data, base) {
     addItems: function (data, base) {
@@ -175,23 +169,6 @@ module.exports = {
         }
         }
     },
     },
 
 
-    /**
-     *  Notify parent compiler that new items
-     *  have been added to the collection, it needs
-     *  to re-calculate computed property dependencies.
-     *  Batched to ensure it's called only once every event loop.
-     */
-    changed: function () {
-        if (this.queued) return
-        this.queued = true
-        var self = this
-        utils.nextTick(function () {
-            if (!self.compiler) return
-            self.compiler.parseDeps()
-            self.queued = false
-        })
-    },
-
     /**
     /**
      *  Run a dry build just to collect bindings
      *  Run a dry build just to collect bindings
      */
      */
@@ -231,7 +208,7 @@ module.exports = {
         }
         }
 
 
         // check if data already exists in the old array
         // check if data already exists in the old array
-        oldIndex = self.old ? indexOf(self.old, data) : -1
+        oldIndex = self.oldVMs ? indexOf(self.oldVMs, data) : -1
         existing = oldIndex > -1
         existing = oldIndex > -1
 
 
         if (existing) {
         if (existing) {
@@ -419,9 +396,10 @@ function objectToArray (obj) {
  *  Find an object or a wrapped data object
  *  Find an object or a wrapped data object
  *  from an Array
  *  from an Array
  */
  */
-function indexOf (arr, obj) {
-    for (var i = 0, l = arr.length; i < l; i++) {
-        if (arr[i] === obj || (obj.$value && arr[i].$value === obj.$value)) {
+function indexOf (vms, obj) {
+    for (var vm, i = 0, l = vms.length; i < l; i++) {
+        vm = vms[i]
+        if (!vm.$reused && (vm.$data === obj || vm.$value === obj)) {
             return i
             return i
         }
         }
     }
     }
@@ -436,7 +414,7 @@ function destroyVMs (vms) {
     while (i--) {
     while (i--) {
         vm = vms[i]
         vm = vms[i]
         if (vm.$reused) {
         if (vm.$reused) {
-            vm.$reused = false
+            delete vm.$reused
         } else {
         } else {
             vm.$destroy()
             vm.$destroy()
         }
         }

+ 6 - 2
src/observer.js

@@ -89,8 +89,12 @@ function linkArrayElements (arr, items) {
         while (i--) {
         while (i--) {
             item = items[i]
             item = items[i]
             if (isWatchable(item)) {
             if (isWatchable(item)) {
-                convert(item)
-                watch(item)
+                // if object is not converted for observing
+                // convert it...
+                if (!item.__emitter__) {
+                    convert(item)
+                    watch(item)
+                }
                 owners = item.__emitter__.owners
                 owners = item.__emitter__.owners
                 if (owners.indexOf(arr) < 0) {
                 if (owners.indexOf(arr) < 0) {
                     owners.push(arr)
                     owners.push(arr)