Live data from Hacker News

Laconic: A Sane Method of Generating DOM Content in JavaScript

joestelmach.github.com

21–30 of 51 posts

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#21
post #11

Earlier quoted context omitted.

Many would frown upon polluting the global name space with functions corresponding to all known HTML tags.

If you end up wrapping your code in an immediately called lambda anyhow I'm sure it would be fine.

Sort of, but there are some tags you probably don't want to pollute your own top-level namespace with, like a, b, i, object, p, q, and s. There's at least one tag (var) which is not allowed as an identifier on its own.

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#22
Just for fun, here's my version from 2006:

http://mg.to/2006/02/27/easy-dom-creation-for-jquery-and-pro...

It's based on Bob Ippolito's version from 2005. :-)

The comments on the post have a few other interesting implementations that others contributed.

At the time I got pretty excited about this idea, but I abandoned it after a while for performance reasons. I was building large DOM structures and it was really slow. I knew that innerHTML was faster, so I tried keeping the same syntax and generating HTML from it instead of DOM insertions - and it was almost as slow that way!

Then I realized that it was just the sheer amount of JavaScript code being run that was slowing it down. I changed my code to build HTML strings with array pushes and a join at the end and then innerHTML to insert, and it was much faster.

Of course, JavaScript has gotten a lot faster since then (at least in new browsers!), so the more code-intensive techniques may be more practical now.

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#23
This is fine for small, one-person projects, but if you generate DOM content in your JS views it will come back to bite you in the ass once you hit a certain size. The first time you need to bring in a designer, or integrate a static mockup from a designer, you'll be wishing you used a template engine.

You wouldn't embed database queries into your view controllers, so why would you couple your html to your javascript?

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#24
post #19

Earlier quoted context omitted.

While I don't entirely agree with your first statement, you could just grab the outerHTML instead of appending the element directly to the DOM: $.el.div($.el.span()).outerHTML

Seems like a reasonable solution. My first statement was certainly true in the previous generation of browsers, including IE 7. I'm not sure about more modern browsers. What basis do you have for disagreeing? Refer to: http://www.quirksmode.org/dom/innerhtml.html

I'm only disagreeing with the use of the word 'performant', and the popular viewpoint that any solution that isn't as fast as possible can't be labeled as such.

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#25
post #10

This is pretty, but unfortunately the only performant way to generate dom elements in javascript is with HTML. HTML parsing is orders of magnitude faster in most browsers. How about a library like this to generate a string of HTML instead of dom elements?

I tried that some years ago (see my other comment). I had a very similar DOM creation function and I updated it to generate HTML instead. It was almost as slow as before - and then I realized that the sheer amount of JS code I was running to generate the HTML was taking up the time.

Of course the performance tradeoff would be different in modern browsers.

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#26
post #23

This is fine for small, one-person projects, but if you generate DOM content in your JS views it will come back to bite you in the ass once you hit a certain size. The first time you need to bring in a designer, or integrate a static mockup from a designer, you'll be wishing you used a template engine. You wouldn't embed database queries into your view controllers, so why would you couple your html to your javascript…

Thanks for the tip, but I've had much success using this method on teams of 5+ developers and a full-time designer.

Re: Laconic: A Sane Method of Generating DOM Content in JavaScript

#28
post #23

This is fine for small, one-person projects, but if you generate DOM content in your JS views it will come back to bite you in the ass once you hit a certain size. The first time you need to bring in a designer, or integrate a static mockup from a designer, you'll be wishing you used a template engine. You wouldn't embed database queries into your view controllers, so why would you couple your html to your javascript…

Thanks for the tip, but I've had much success using this method on teams of 5+ developers and a full-time designer.

Obviously you should do whatever works for your team, and I'm not commenting on the quality or necessity of your framework. In my opinion, it's just much easier to edit well formatted markup, verbose as it may be, than wading through a forest of $el and appendTo and such. Not to mention, hiring a talented visual designer that knows html & css is much easier than finding one that knows their way around a client-side MVC framework. Unicorns and all that. To each his own.
Post reply on HN