Skip to content

Commit 588b633

Browse files
committed
support "item in array" syntax
1 parent 171fdc7 commit 588b633

File tree

3 files changed

+72
-42
lines changed

3 files changed

+72
-42
lines changed

src/directive.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ function Directive (name, el, vm, descriptor, def, host) {
2727
this.name = name
2828
this.el = el
2929
this.vm = vm
30+
if (def._guard) {
31+
def._guard(descriptor)
32+
}
3033
// copy descriptor props
3134
this.raw = descriptor.raw
3235
this.expression = descriptor.expression

src/directives/repeat.js

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -524,48 +524,6 @@ module.exports = {
524524
}
525525
},
526526

527-
/**
528-
* Pre-process the value before piping it through the
529-
* filters, and convert non-Array objects to arrays.
530-
*
531-
* This function will be bound to this directive instance
532-
* and passed into the watcher.
533-
*
534-
* @param {*} value
535-
* @return {Array}
536-
* @private
537-
*/
538-
539-
_preProcess: function (value) {
540-
// regardless of type, store the un-filtered raw value.
541-
this.rawValue = value
542-
var type = this.rawType = typeof value
543-
if (!isPlainObject(value)) {
544-
this.converted = false
545-
if (type === 'number') {
546-
value = range(value)
547-
} else if (type === 'string') {
548-
value = _.toArray(value)
549-
}
550-
return value || []
551-
} else {
552-
// convert plain object to array.
553-
var keys = Object.keys(value)
554-
var i = keys.length
555-
var res = new Array(i)
556-
var key
557-
while (i--) {
558-
key = keys[i]
559-
res[i] = {
560-
$key: key,
561-
$value: value[key]
562-
}
563-
}
564-
this.converted = true
565-
return res
566-
}
567-
},
568-
569527
/**
570528
* Insert an instance.
571529
*
@@ -666,6 +624,64 @@ module.exports = {
666624
return hook
667625
? hook.call(vm, index, total)
668626
: index * this[type]
627+
},
628+
629+
/**
630+
* Pre-process the value before piping it through the
631+
* filters, and convert non-Array objects to arrays.
632+
*
633+
* This function will be bound to this directive instance
634+
* and passed into the watcher.
635+
*
636+
* @param {*} value
637+
* @return {Array}
638+
* @private
639+
*/
640+
641+
_preProcess: function (value) {
642+
// regardless of type, store the un-filtered raw value.
643+
this.rawValue = value
644+
var type = this.rawType = typeof value
645+
if (!isPlainObject(value)) {
646+
this.converted = false
647+
if (type === 'number') {
648+
value = range(value)
649+
} else if (type === 'string') {
650+
value = _.toArray(value)
651+
}
652+
return value || []
653+
} else {
654+
// convert plain object to array.
655+
var keys = Object.keys(value)
656+
var i = keys.length
657+
var res = new Array(i)
658+
var key
659+
while (i--) {
660+
key = keys[i]
661+
res[i] = {
662+
$key: key,
663+
$value: value[key]
664+
}
665+
}
666+
this.converted = true
667+
return res
668+
}
669+
},
670+
671+
/**
672+
* Internal hook to do custom transform on the directive's
673+
* descriptor so that it can support special syntax.
674+
*
675+
* @param {Object} descriptor
676+
*/
677+
678+
_guard: function (descriptor) {
679+
var exp = descriptor.expression
680+
var match = exp.trim().match(/(.*) in (.*)/)
681+
if (match) {
682+
descriptor.arg = match[1]
683+
descriptor.expression = match[2]
684+
}
669685
}
670686

671687
}

test/unit/specs/directives/repeat_spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ if (_.inBrowser) {
4343
assertMutations(vm, el, done)
4444
})
4545

46+
it('item in list syntax', function (done) {
47+
var vm = new Vue({
48+
el: el,
49+
data: {
50+
items: [{a: 1}, {a: 2}]
51+
},
52+
template: '<div v-repeat="item in items">{{$index}} {{item.a}}</div>'
53+
})
54+
assertMutations(vm, el, done)
55+
})
56+
4657
it('primitive with identifier', function (done) {
4758
var vm = new Vue({
4859
el: el,

0 commit comments

Comments
 (0)