luci-base: cbi.js: add heuristics to attribute handling in E()

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>
This commit is contained in:
Jo-Philipp Wich 2018-11-05 11:11:46 +01:00
parent 9b20f7ac52
commit f4c39dd6ff

View file

@ -1506,7 +1506,18 @@ function E()
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);