-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Added support for iterable objects in {{#each}} helper #1557
Conversation
Currently {{#each}} helper supports either arrays, or objects, however nowadays you can define custom iterable objects by overriding a special method called Symbol.iterator, which results in empty result being rendered.
Looks interesting. I was under the impression that generator-functions were only used for asynchronous javascript (which Handlebars does not support). I would like to wait for @wycats approval to merge this, because he wanted no new features until we have spec. I don't know if that applies here, because this is just a helper being adapted a little, and other programming languages don't have iterators. But still... |
Actually iterator doesn't have to be a generator function, it must comply to the iterator protocol, generators are just an easier way to do this. Inside it can be a normal class, ES5 function, plain object, etc... Here's an example of implementing an iterable object without using generators, and using it in two ways: by creating an array with class MyIterator {
constructor(arr) {
this.arr = arr;
this.index = 0;
}
next() {
const value = this.arr[this.index];
const done = this.index === this.arr.length;
if (!done) {
this.index++;
}
return { value, done };
}
}
class MyIterable {
constructor(arr) {
this.arr = arr;
}
[Symbol.iterator]() {
return new MyIterator(this.arr);
}
}
const my = new MyIterable(['a', 'b', 'c']);
// using the iterator with ES6
console.log([...my]);
// using the iterator manually
const it = my[Symbol.iterator]();
for (let current = it.next(); !current.done; current = it.next()) {
console.log(current.value);
} |
I've been thinking about this PR, and it is probably save to merge this. But your tests will fail in saucelabs with IE11, because "Symbol.iterator" is not supported there, and we currently do not transpile the tests. So either we build I would prefer the first solution, and I have thought about refactoring the whole test mechanism (to use |
Thanks, didn't know tests are not transpiled. But the code is transpiled I believe, right? Or should I also rewrite the code in ES5? It's using |
Yes, the code is transpiled, but most likely we don't want to include the polyfill into the runtime just because of this one helper. So, I've rewritten it in ES5 as well 😉 |
when the build is run in a local branch, it will be run in Saucelabs in a variety of browsers. Oh, and please remind me again, if I haven't done so by next Monday. I have forgotten about some PRs in the past and I don't want this lying around for months. |
released in 4.4.0 |
Thanks a lot! |
Currently {{#each}} helper supports either arrays, or objects,
however nowadays you can define custom iterable objects by overriding
a special method called Symbol.iterator, which results in empty result
being rendered.
4.x
-branch contains the latest version. Please target that branch in the PR.