2
0
Evan You 10 жил өмнө
parent
commit
90f59dbeb1
1 өөрчлөгдсөн 44 нэмэгдсэн , 0 устгасан
  1. 44 0
      src/directives/for.js

+ 44 - 0
src/directives/for.js

@@ -71,6 +71,13 @@ module.exports = {
   },
 
   update: function (data) {
+    this.diff(data)
+    if (this.refId) {
+      this.updateRef()
+    }
+  },
+
+  diff: function (data) {
     var idKey = this.idKey
     var converted = this.converted
     var oldFrags = this.frags
@@ -160,6 +167,17 @@ module.exports = {
     }
   },
 
+  updateRef: function () {
+    if (!this.converted) {
+      this.vm.$[this.refId] = this.frags.map(findVmFromFrag)
+    } else {
+      var refs = this.vm.$[this.refId] = {}
+      this.frags.forEach(function (frag) {
+        refs[frag.scope.$key] = findVmFromFrag(frag)
+      })
+    }
+  },
+
   insert: function (frag, index, prevEl, inDoc) {
     if (frag.staggerCb) {
       frag.staggerCb.cancel()
@@ -346,6 +364,21 @@ module.exports = {
       this.converted = true
       return res
     }
+  },
+
+  unbind: function () {
+    if (this.refId) {
+      this.vm.$[this.redId] = null
+    }
+    if (this.frags) {
+      var i = this.frags.length
+      var frag
+      while (i--) {
+        frag = this.frags[i]
+        this.deleteCachedFrag(frag)
+        frag.unlink()
+      }
+    }
   }
 }
 
@@ -380,6 +413,17 @@ function findPrevFrag (frag, anchor, id) {
   return frag
 }
 
+/**
+ * Find a vm from a fragment.
+ *
+ * @param {Fragment} frag
+ * @return {Vue|undefined}
+ */
+
+function findVmFromFrag (frag) {
+  return frag.node.__vue__ || frag.node.nextSibling.__vue__
+}
+
 /**
  * Create a range array from given number.
  *