Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Iterator supports for v-for #7171

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/core/instance/render-helpers/render-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,25 @@ export function renderList (
}
} else if (isObject(val)) {
keys = Object.keys(val)
ret = new Array(keys.length)
for (i = 0, l = keys.length; i < l; i++) {
key = keys[i]
ret[i] = render(val[key], key, i)
if (keys.length === 0 && val.toString().indexOf('Iterator') > -1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterator looks like new to me. Would you like to give me a link of introduction to Iterator? I might be wrong but Google only gave me Generator.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Map.prototype.keys() =>return a new Map iterator object.
  2. Set.prototype.values() => return a new Iterator object containing the values for each element in the given Set, in insertion order.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I have checked the spec and MapIterator's @@toStringTag symbol returns Map Iterator. The similar for other iterators.

GIven this, I suspect if a toString test is legitimate. Checking Symbol.iterator in proper environment is better, I think.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the better way is checking Symbol.iterator. At first I thought I can't use the ES6 grammar.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symbol.toStringTag
A string value used for the default description of an object. Used by Object.prototype.toString().
Symbol

ret = []
i = 0
while (true) {
if (typeof val.next !== 'function') {
break
}
const next = val.next()
if (next.done) {
break
}
ret.push(render(next.value, i++))
}
} else {
ret = new Array(keys.length)
for (i = 0, l = keys.length; i < l; i++) {
key = keys[i]
ret[i] = render(val[key], key, i)
}
}
}
if (isDef(ret)) {
Expand Down