Skip to content

Commit 506cf13

Browse files
committed
allow v-repeat to work with numbers and strings
1 parent 0e81f73 commit 506cf13

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

src/directives/repeat.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ module.exports = {
133133
*/
134134

135135
update: function (data) {
136-
if (!data) return
136+
if (typeof data === 'number') {
137+
data = range(data)
138+
}
137139
this.converted = data && data._converted
138140
this.vms = this.diff(data || [], this.vms)
139141
// update v-ref
@@ -455,16 +457,8 @@ function findNextVm (vm, ref) {
455457
*/
456458

457459
function objToArray (obj) {
458-
if (_.isArray(obj)) {
459-
return obj
460-
}
461-
if (obj == null) return
462460
if (!_.isPlainObject(obj)) {
463-
_.warn(
464-
'Invalid value for v-repeat: ' + obj +
465-
'\nOnly Arrays and Objects are allowed.'
466-
)
467-
return
461+
return obj
468462
}
469463
var keys = Object.keys(obj)
470464
var i = keys.length
@@ -479,4 +473,13 @@ function objToArray (obj) {
479473
}
480474
res._converted = true
481475
return res
476+
}
477+
478+
function range (n) {
479+
var i = -1
480+
var ret = new Array(n)
481+
while (++i < n) {
482+
ret[i] = i
483+
}
484+
return ret
482485
}

test/unit/specs/directives/repeat_spec.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,35 +321,31 @@ if (_.inBrowser) {
321321
expect(_.warn).toHaveBeenCalled()
322322
})
323323

324-
it('warn invalid data type', function () {
324+
it('warn v-if', function () {
325325
var vm = new Vue({
326326
el: el,
327-
template: '<div v-repeat="items"></div>',
327+
template: '<div v-repeat="items" v-if="aaa"></div>',
328328
data: {
329-
items: 1234
329+
items: []
330330
}
331331
})
332332
expect(_.warn).toHaveBeenCalled()
333333
})
334334

335-
it('warn v-if', function () {
335+
it('repeat number', function () {
336336
var vm = new Vue({
337337
el: el,
338-
template: '<div v-repeat="items" v-if="aaa"></div>',
339-
data: {
340-
items: []
341-
}
338+
template: '<div v-repeat="3">{{$index}} {{$value}}</div>'
342339
})
343-
expect(_.warn).toHaveBeenCalled()
340+
expect(el.innerHTML).toBe('<div>0 0</div><div>1 1</div><div>2 2</div><!--v-repeat-->')
344341
})
345342

346-
it('falsy value', function () {
343+
it('repeat string', function () {
347344
var vm = new Vue({
348345
el: el,
349-
template: '<div v-repeat="items"></div>',
350-
data: {}
346+
template: '<div v-repeat="\'vue\'">{{$index}} {{$value}}</div>'
351347
})
352-
expect(_.warn).not.toHaveBeenCalled()
348+
expect(el.innerHTML).toBe('<div>0 v</div><div>1 u</div><div>2 e</div><!--v-repeat-->')
353349
})
354350

355351
it('teardown', function () {

0 commit comments

Comments
 (0)