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

allow optional `(key,val) in arr` syntax for `v-for`

optionally binding the iteration index (or key when iterating over an
object) to a name to simplify work with nested `v-for`s
vprimachenko 10 лет назад
Родитель
Сommit
7e5577cf3e
1 измененных файлов с 10 добавлено и 1 удалено
  1. 10 1
      src/directives/public/for.js

+ 10 - 1
src/directives/public/for.js

@@ -11,7 +11,13 @@ module.exports = {
     // support "item in items" syntax
     var inMatch = this.expression.match(/(.*) in (.*)/)
     if (inMatch) {
-      this.alias = inMatch[1]
+      var itMatch = inMatch[1].match(/\((.*),(.*)\)/)
+      if (itMatch) {
+        this.iterator = itMatch[1]
+        this.alias = itMatch[2]
+      } else {
+        this.alias = inMatch[1]
+      }
       this.expression = inMatch[2]
     }
 
@@ -207,6 +213,9 @@ module.exports = {
       // avoid accidental fallback
       _.define(scope, '$key', null)
     }
+    if (this.iterator) {
+      _.defineReactive(scope, this.iterator, key || index)
+    }
     var frag = this.factory.create(host, scope, this._frag)
     frag.forId = this.id
     this.cacheFrag(value, frag, index, key)