If a given attribute value is a function, register it as event listener,
if it is an object, filter it through JSON.stringify(), else set it
as-is.
This helps to reduce some boiler-plate code when building DOM structures.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
if (attr)
for (var key in attr)
if (attr.hasOwnProperty(key) && attr[key] !== null && attr[key] !== undefined)
- elem.setAttribute(key, attr[key]);
+ switch (typeof(attr[key])) {
+ case 'function':
+ elem.addEventListener(key, attr[key]);
+ break;
+
+ case 'object':
+ elem.setAttribute(key, JSON.stringify(attr[key]));
+ break;
+
+ default:
+ elem.setAttribute(key, attr[key]);
+ }
if (typeof(data) === 'function')
data = data(elem);