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:
parent
9b20f7ac52
commit
f4c39dd6ff
1 changed files with 12 additions and 1 deletions
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue