Re: [w3c/webcomponents] Discussion: Templates that Don't Destroy Element References (#777)

@matthewp : When you create a string, it becomes a [string object instance](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Useful_string_methods) having numerous methods. Since converting strings to DOM is such a common task in client-side development, it seems quite natural to me that browsers would add a `.toDOM()` prototype method there along with all the other string methods.

Tag Template literals are a wonderful but strange beast in JavaScript; somewhere between the template literal itself and the tag function, a magic hidden splitter-function occurs that feeds the template literal to your tag function in pieces: `let template = function(aryStatic, ...expressionResults){}`. It seems to me that similar Magic would be possible for a prototype method.

You mentioned the window object, while my first though of an alternative would be for browsers to add a class-method on String: 
`let element  = String.toDOM(`<h1>String with ${expression}.</h1>`);`

 @justinfagnani : Actually, I must admit that the `html` template tag in lit-html is quite elegant. This talk about the exact syntax of things has gotten me on a tangent away from my main point Which is:

If I do not want an updat-able template . . . if all I want is DOM from the template tag function, then why must I call a template function that returns a template result which is NOT DOM? It seems like some efficiency would be gained from not returning something that facilitates more than I need.

In my web components, I create the dynamic elements manually, so that I'll have direct object references to each thing that needs to change. When my web component receives an event from the browser, methods get fired and the only thing updated is exactly what changed (without having to re-evaluate a template at all). So, the only thing I want, is the ability to convert a template literal directly into DOM.

I realize that Template Instantiation, lit-html, and hyperHTML all allow me to do just that, but its done at the price of what it costs to make a fully functional update-able template, and I'd like to do it at the cost (in memory and performance) to just generate the DOM (once, and garbage collect the rest). Surely, more resources are required to make a fully functional, update-able template, compared to a tag function that is elusively dedicated to only creating DOM once? No?
`let element = DOMonly`<h1>String with ${expression}.</h1>`;  //returns DOM without template.`

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webcomponents/issues/777#issuecomment-445367374

Received on Friday, 7 December 2018 21:14:45 UTC