From f4c39dd6ff6f60dc48982d822faa6d9a85c2e4d7 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Mon, 5 Nov 2018 11:11:46 +0100 Subject: [PATCH] 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 --- .../luci-base/htdocs/luci-static/resources/cbi.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/modules/luci-base/htdocs/luci-static/resources/cbi.js b/modules/luci-base/htdocs/luci-static/resources/cbi.js index 3ace96f32..d9b9baf7b 100644 --- a/modules/luci-base/htdocs/luci-static/resources/cbi.js +++ b/modules/luci-base/htdocs/luci-static/resources/cbi.js @@ -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); -- 2.25.1