Skip to content

Commit 1fd199e

Browse files
vprimachenkoyyx990803
authored andcommitted
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
1 parent d58c7e4 commit 1fd199e

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/directives/for.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ module.exports = {
1414
// support "item in items" syntax
1515
var inMatch = this.expression.match(/(.*) in (.*)/)
1616
if (inMatch) {
17-
this.alias = inMatch[1]
17+
var itMatch = inMatch[1].match(/\((.*),(.*)\)/)
18+
if (itMatch) {
19+
this.iterator = itMatch[1]
20+
this.alias = itMatch[2]
21+
} else {
22+
this.alias = inMatch[1]
23+
}
1824
this._watcherExp = inMatch[2]
1925
}
2026

@@ -215,6 +221,9 @@ module.exports = {
215221
// avoid accidental fallback
216222
_.define(scope, '$key', null)
217223
}
224+
if (this.iterator) {
225+
_.defineReactive(scope, this.iterator, key || index)
226+
}
218227
var frag = this.factory.create(host, scope, this._frag)
219228
frag.forId = this.id
220229
this.cacheFrag(value, frag, index, key)

0 commit comments

Comments
 (0)