Created
December 19, 2011 21:32
-
-
Save futuredarrell/1498982 to your computer and use it in GitHub Desktop.
Trying to map element functions to scope of function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var html = function(template){ | |
var fragment = document.createDocumentFragment() | |
, body = template.toString() | |
, num = body.indexOf('{') | |
, d = {} | |
, emap | |
, render | |
, elements; | |
// the ugly | |
elements = ['a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', | |
'b', 'bdi', 'bdo', 'blockquote', 'br', 'button', 'canvas', 'caption', 'cite', | |
'code', 'col', 'colgroup', 'command', 'datalist', 'dd', 'del', 'details', | |
'device','dfn', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', | |
'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', | |
'hgroup', 'hr', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', | |
'label', 'legend', 'li', 'link', 'map', 'mark', 'menu', 'meter', 'nav', | |
'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', | |
'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', | |
'select', 'small', 'source', 'span', 'strong', 'style', 'sub', 'summary', | |
'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', | |
'tr', 'track', 'ul', '_var', 'video', 'wbr']; | |
// get rid of lead brace | |
body = body.slice(++num, -1); | |
//TODO: add onclick methods | |
render = function(node, args){ | |
args.forEach(function(e){ | |
if(typeof e == undefined) return false; | |
if(typeof e == 'string'){ | |
var text = document.createTextNode(e); | |
node.appendChild(text); | |
} else if (e instanceof HTMLElement){ | |
node.appendChild(e); | |
} else if (typeof e == 'object'){ | |
for(var attrs in e){ | |
node.setAttribute(attrs, e[attrs]); | |
} | |
} | |
}); | |
fragment.appendChild(node); | |
return node; | |
}; | |
//object for dom methods | |
elements.forEach(function(el){ | |
d[el] = function(){ | |
render.call(null, document.createElement(el), Array.prototype.slice.call(arguments)); | |
}; | |
}); | |
emap = elements.map(function(el){ | |
return d[el]; | |
}); | |
var c = new Function('a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', | |
'b', 'bdi', 'bdo', 'blockquote', 'br', 'button', 'canvas', 'caption', 'cite', | |
'code', 'col', 'colgroup', 'command', 'datalist', 'dd', 'del', 'details', | |
'device','dfn', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', | |
'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', | |
'hgroup', 'hr', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', | |
'label', 'legend', 'li', 'link', 'map', 'mark', 'menu', 'meter', 'nav', | |
'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', | |
'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', | |
'select', 'small', 'source', 'span', 'strong', 'style', 'sub', 'summary', | |
'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', | |
'tr', 'track', 'ul', '_var', 'video', 'wbr', body).apply(null, emap); | |
return fragment; | |
}; | |
var template = function(){ | |
div({'id':'something', 'class': 'something'}, | |
p({'id':'yeah'}, 'text2'), | |
p('text3'), | |
p('text4'), | |
p('text5') | |
); | |
p({'id':'something-else'}, 'test'); | |
_var('meh'); | |
}; | |
document.body.appendChild(html(template)); |
yes, what's actually bad in that approach, that I've noticed now, is that, that template function will lexically land outside of closure in which it's originally declared e.g.:
var predefObj = { txt: message };
var template = function () {
p(predefObj.message);
};
html(template); // predefinedObj is undefined
And limiting us from using closures is big limitation. I think dynamic scope'ing as it's now in domjs is a bit nicer, and It's probably impossible to write really convenient solution that wouldn't introduce build step :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a pretty weak implementation of this idea. I'm not sure how to pass through variables in this way, or if its even worth pursuing.