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

support "item in array" syntax

Evan You 11 лет назад
Родитель
Сommit
588b633361
3 измененных файлов с 72 добавлено и 42 удалено
  1. 3 0
      src/directive.js
  2. 58 42
      src/directives/repeat.js
  3. 11 0
      test/unit/specs/directives/repeat_spec.js

+ 3 - 0
src/directive.js

@@ -27,6 +27,9 @@ function Directive (name, el, vm, descriptor, def, host) {
   this.name = name
   this.el = el
   this.vm = vm
+  if (def._guard) {
+    def._guard(descriptor)
+  }
   // copy descriptor props
   this.raw = descriptor.raw
   this.expression = descriptor.expression

+ 58 - 42
src/directives/repeat.js

@@ -524,48 +524,6 @@ module.exports = {
     }
   },
 
-  /**
-   * Pre-process the value before piping it through the
-   * filters, and convert non-Array objects to arrays.
-   *
-   * This function will be bound to this directive instance
-   * and passed into the watcher.
-   *
-   * @param {*} value
-   * @return {Array}
-   * @private
-   */
-
-  _preProcess: function (value) {
-    // regardless of type, store the un-filtered raw value.
-    this.rawValue = value
-    var type = this.rawType = typeof value
-    if (!isPlainObject(value)) {
-      this.converted = false
-      if (type === 'number') {
-        value = range(value)
-      } else if (type === 'string') {
-        value = _.toArray(value)
-      }
-      return value || []
-    } else {
-      // convert plain object to array.
-      var keys = Object.keys(value)
-      var i = keys.length
-      var res = new Array(i)
-      var key
-      while (i--) {
-        key = keys[i]
-        res[i] = {
-          $key: key,
-          $value: value[key]
-        }
-      }
-      this.converted = true
-      return res
-    }
-  },
-
   /**
    * Insert an instance.
    *
@@ -666,6 +624,64 @@ module.exports = {
     return hook
       ? hook.call(vm, index, total)
       : index * this[type]
+  },
+
+  /**
+   * Pre-process the value before piping it through the
+   * filters, and convert non-Array objects to arrays.
+   *
+   * This function will be bound to this directive instance
+   * and passed into the watcher.
+   *
+   * @param {*} value
+   * @return {Array}
+   * @private
+   */
+
+  _preProcess: function (value) {
+    // regardless of type, store the un-filtered raw value.
+    this.rawValue = value
+    var type = this.rawType = typeof value
+    if (!isPlainObject(value)) {
+      this.converted = false
+      if (type === 'number') {
+        value = range(value)
+      } else if (type === 'string') {
+        value = _.toArray(value)
+      }
+      return value || []
+    } else {
+      // convert plain object to array.
+      var keys = Object.keys(value)
+      var i = keys.length
+      var res = new Array(i)
+      var key
+      while (i--) {
+        key = keys[i]
+        res[i] = {
+          $key: key,
+          $value: value[key]
+        }
+      }
+      this.converted = true
+      return res
+    }
+  },
+
+  /**
+   * Internal hook to do custom transform on the directive's
+   * descriptor so that it can support special syntax.
+   *
+   * @param {Object} descriptor
+   */
+
+  _guard: function (descriptor) {
+    var exp = descriptor.expression
+    var match = exp.trim().match(/(.*) in (.*)/)
+    if (match) {
+      descriptor.arg = match[1]
+      descriptor.expression = match[2]
+    }
   }
 
 }

+ 11 - 0
test/unit/specs/directives/repeat_spec.js

@@ -43,6 +43,17 @@ if (_.inBrowser) {
       assertMutations(vm, el, done)
     })
 
+    it('item in list syntax', function (done) {
+      var vm = new Vue({
+        el: el,
+        data: {
+          items: [{a: 1}, {a: 2}]
+        },
+        template: '<div v-repeat="item in items">{{$index}} {{item.a}}</div>'
+      })
+      assertMutations(vm, el, done)
+    })
+
     it('primitive with identifier', function (done) {
       var vm = new Vue({
         el: el,