Laconic: A Sane Method of Generating DOM Content in JavaScript
joestelmach.github.com
joestelmach.github.com
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.
JavaScript has come a long way, even from 2006, yet most developers still have the mindset that this sort of thing can't perform well. What we're left with are solutions that are more complex, and perform similarly for most applications.
You wouldn't embed database queries into your view controllers, so why would you couple your html to your javascript?
But I think your point about designers is a valid one for many projects.
Using `Object.prototype.toString.call` is also a pretty dirty hack, especially to detect an "array". I think `typeof obj === "object" && obj.length` would suffice, even if certain false positives like string objects leak through.
[0]: http://i.imgur.com/RNBYd.png
[1]: http://msdn.microsoft.com/en-us/library/dd347148(v=VS.85).as...
Object.prototype.toString ( ) When the toString method is called, the following steps are taken:
1. If the this value is undefined, return "[object Undefined]".
2. If the this value is null, return "[object Null]".
3. Let O be the result of calling ToObject passing the this value as the argument.
4. Let class be the value of the [[Class]] internal property of O.
5. Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".
This is because there is no defined standard for `toString` results of host objects.
div(span('hello world')).inject(document.body);
Very simple to do and even simpler to use.What's more of a problem is if you have something like this: https://gist.github.com/2499913
The problem is that the "var p" in the for loop will be hoisted to the top of the scope, and so when you call p() in the first line, it will actually be undefined.
The problem I see is that this only works for "all known HTML tags." If you want to use custom tags, like <template> (I believe that's what meteor uses), this won't cut it since it uses $.el.tagname syntax, every tag has to be loaded into the $.el object.
How about a very similar, more jQuery-like syntax instead:
$.el('template', [
$.el('div', {'class' : 'foo'}, [
//other children here
//so basically, the last arg is always an array of children
]
]); $.el.div({className : 'foo'});
$.el('div', {className : 'foo'});http://blog.fastmail.fm/2012/02/20/building-the-new-ajax-mai...
Used the CSS syntax to easily set class/id on a newly created tag.
Benchmarks included to show it's just as fast as innerHTML on modern browsers.
https://github.com/petehunt/htmldry/blob/master/demos/events...
It's a little more HAML-inspired though.
It also allows you to generate HTML strings from the same code and create templates, with template inheritance, using the same sort of syntax.
I much prefer CoffeeScript, because plain JS makes me mad, and CoffeeKup (http://coffeekup.org/) for generating DOM content.
It's a serious question. What benefit is there to doing this?
The only thing that came to mind is now you have the dom node so you can bind events to it. But you're not going to be doing that everywhere. So why make things more complicated?
How about a library like this to generate a string of HTML instead of dom elements?
$.el.div($.el.span()).outerHTMLMy 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:
Of course the performance tradeoff would be different in modern browsers.
Thanks!