-
Notifications
You must be signed in to change notification settings - Fork 13
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
Overloaded return type seems problematic #6
Comments
I've been thinking about this myself. I like the idea of being able to create sibling nodes when that is your intension, but the fact that you don't always know what will result in an array being returned makes it a bit difficult. I wouldn't mind returning the DocumentFragment, but I don't know if that would be what someone who first uses the API would expect. Always returning an array isn't a bad idea either, but the interface for appending it do the DOM is a not friendly towards arrays. If we could add a new API similar to |
Returning a In addition to that |
Being able to call |
However, would it be an odd interface to have to always find the node you want when you want to attach events to the returned value if we use a DocumentFragment? var el = html`<button class="btn">click me</button>`;
// adds the event listener to the DocumentFragment and not the button
el.addEventListener('click', function() {
console.log('clicked');
});
el.click(); // doesn't work
// now I added the event to the button
el.querySelector('button').addEventListener('click', function() {
console.log('clicked');
});
el.firstChild.click(); // works
document.body.appendChild(el); |
I do agree there is some definite convenience in getting a single node instead of a document fragment. A document fragment is good in many cases but as you point out, it's not great in others. I guess I kind of tend toward the two-tags solution. Maybe that is too confusing though and just asking people to do |
Is there a way we could have the best of all worlds? I love that you can call @jshcrowthe suggested that maybe the documentFragment could pass through all event-like actions to the first child node, since a documentFragment never gets added to the page any events attached to it seem pointless. I'm not sure how you would make the distinction of which APIs to proxy through, since maybe the user wanted to call This is definitely a hard problem. I'm torn between a consistent API and the convenience of an overloaded API. |
So after a long while letting this just sit in the back of my mind, I'm thinking we should go with two separate functions. One that always returns a single node and one that always returns a doc fragment for multiple sibling nodes. This mimics the behavior of I'm not sure what we should call the 2nd function for sibling nodes though. |
Closing as it seems returning a single node for one child and a doc fragment for multiple children hasn't been a problem. Please reopen if further discussion is needed. |
Moving the discussion from whatwg/dom#150 (comment).
I see a few alternatives:
htmls
??) that always returns an array of nodesMaybe it's not too bad to have the overloaded return type, since users are always in control of its usage and it should be pretty obvious what the return type is? That is, there's no way to feed user input into a template string, so it should be pretty obvious from source inspection whether you're going to get multiple nodes or one node.
On the other hand, maybe it's not obvious:
html
foo
`` creates two nodes, due to the leading whitespace. Even worse,creates three nodes. Having that throw might be better than having it silently become an array.
The text was updated successfully, but these errors were encountered: