Evan You преди 11 години
родител
ревизия
03c72f2b25
променени са 5 файла, в които са добавени 101 реда и са изтрити 4 реда
  1. 1 2
      src/directives/component.js
  2. 1 2
      src/directives/partial.js
  3. 80 0
      src/directives/repeat.js
  4. 13 0
      src/util/dom.js
  5. 6 0
      test/unit/specs/util/dom_spec.js

+ 1 - 2
src/directives/component.js

@@ -25,8 +25,7 @@ module.exports = {
     if (!this.el.__vue__) {
       // create a ref anchor
       this.ref = document.createComment('v-component')
-      _.before(this.ref, this.el)
-      _.remove(this.el)
+      _.replace(this.el, this.ref)
       // check v-if conditionals
       this.checkIf()
       // check keep-alive options

+ 1 - 2
src/directives/partial.js

@@ -18,8 +18,7 @@ module.exports = {
     if (el.nodeType === 8) {
       // keep a ref for the partial's content nodes
       var nodes = _.toArray(partial.childNodes)
-      _.before(partial, el)
-      _.remove(el)
+      _.replace(el, partial)
       // compile partial after appending, because its
       // children's parentNode will change from the fragment
       // to the correct parentNode. This could affect

+ 80 - 0
src/directives/repeat.js

@@ -0,0 +1,80 @@
+var _ = require('../util')
+var uid = 0
+
+module.exports = {
+
+  bind: function () {
+    // uid as a cache identifier
+    this.id = '__v_repeat_' + (++uid)
+    // put in the default filter to guard Object values
+    // this filter needs to always be the first one. We
+    // can do this in bind because the watcher is not
+    // created yet.
+    if (!this.filters) {
+      this.filters = []
+    }
+    this.filters.unshift('_objectToArray')
+    // check v-ref
+    var childId = _.attr(this.el, 'ref')
+    this.childId = this.vm.$interpolate(childId)
+    // setup ref node
+    this.ref = document.createComment('v-repeat')
+    _.replace(this.el, this.ref)
+    // instance holders
+    this.data = this.vms = this.oldData = this.oldVms = null
+  },
+
+  update: function (data) {
+    if (!_.isArray(data)) {
+      _.warn('v-repeat expects an Array or Object value.')
+      return
+    }
+    this.oldVms = this.vms
+    this.oldData = this.data
+    // TODO
+  },
+
+  unbind: function () {
+    
+  },
+
+  /**
+   * Save a vm's reference on a data object as a hidden
+   * property. This mimics a Map that allows us to determine
+   * a data object's owner instance during the diff phase.
+   *
+   * @param {Object} data
+   * @param {Vue} vm
+   */
+
+  cacheInstance: function (data, vm) {
+    if (data[this.id] !== undefined) {
+      data[this.id] = vm
+    } else {
+      _.define(data, this.id, vm)
+    }
+  },
+
+  /**
+   * Try to get a cached instance from a piece of data.
+   *
+   * @param {Object} data
+   * @return {Vue|undefined}
+   */
+
+  getInstance: function (data) {
+    return data[this.id]
+  },
+
+  /**
+   * Delete the saved reference on a data object.
+   * This assumes the data already has a vm cached on it.
+   *
+   * @param {Object} data
+   */
+
+  deleteInstance: function (data) {
+    data[this.id] = null
+  }
+
+}

+ 13 - 0
src/util/dom.js

@@ -67,6 +67,19 @@ exports.prepend = function (el, target) {
   }
 }
 
+/**
+ * Replace target with el
+ *
+ * @param {Element} target
+ * @param {Element} el
+ */
+
+exports.replace = function (target, el) {
+  var parent = target.parentNode
+  parent.insertBefore(el, target)
+  parent.removeChild(target)
+}
+
 /**
  * Copy attributes from one element to another.
  *

+ 6 - 0
test/unit/specs/util/dom_spec.js

@@ -63,6 +63,12 @@ if (_.inBrowser) {
       expect(parent.firstChild).toBe(target)
     })
 
+    it('replace', function () {
+      _.replace(child, target)
+      expect(parent.childNodes.length).toBe(1)
+      expect(parent.firstChild).toBe(target)
+    })
+
     it('copyAttributes', function () {
       parent.setAttribute('test1', 1)
       parent.setAttribute('test2', 2)