Question on converting lua app to javascript

@jow @aparcar @dibdot @hnyman I've been working on converting lua apps to javascript and the only hurdle I couldn't overcome is that after a button click I need to refresh either the status widget or the hole page.

This is the overview.js: https://github.com/stangri/source.openwrt.melmac.net/blob/master/luci-app-simple-adblock/htdocs/luci-static/resources/view/simple-adblock/overview.js, this is where the status widget is defined: https://github.com/stangri/source.openwrt.melmac.net/blob/master/luci-app-simple-adblock/htdocs/luci-static/resources/simple-adblock/status.js.

I just don't understand how to cause widget/whole page re-render from here: https://github.com/stangri/source.openwrt.melmac.net/blob/master/luci-app-simple-adblock/htdocs/luci-static/resources/simple-adblock/status.js#L79-L81

Can anyone guide me in the right direction?

Another question on a related subject, within the https://openwrt.github.io/luci/jsapi/LuCI.form.Value.html documentation, there's reference to LuCI.validation:

I can't find the LuCI.validation anywhere in the docs. Is it missing? Am I blind?

1 Like

Just check the source: https://github.com/openwrt/luci/blob/cd06e708d8d8037486b77dab3389ca91a88f2dd0/modules/luci-base/htdocs/luci-static/resources/validation.js#L227

1 Like

This is super-helpful Dirk, thank you so much! I wish I was as competent in navigating sources as you are!

Do you know if it 100% matches uci validation? I remember lua allowing things I couldn't do with uci validation, hence the question.

Haven't checked this but server side checks like directory or files check won't work

I meant if I can just copy-paste the datatypes from luci app into uci_load_validate?

Yes, the same strings should work in both JS and Lua contexts.

Without having studied your code in detail, there's two general approaches:

  1. DOM manipulation, means getElementById(), querySelector() or similar methods to obtain the DOM node representing your status widget and swap/redraw its contents (e.g. in case of HTML tables you could use cbi_update_table())

  2. Full page reload. That would be the "shotgun approach" to simply reload the entire page. It should be possible to trigger that using location.reload();

1 Like

I meant can I transfer this from js to init script?

Ah! Well the uci validation should support the same syntax as well, but it might lack some of the datatypes. Here’s the list of implemented ones:

https://git.openwrt.org/?p=project/ubox.git;a=blob;f=validate/validate.c;h=e72b8117ecd8b680778b0f5c7637ed6546a7736b;hb=4c7b720b9c63b826fb9404e454ae54f2ef5649d5#l893

1 Like

I'm going to mark as a solution the reply on re-render and then combine replies from @dibdot and @jow on validation so that both links are in the single post below:

js/luci validation datatypes: https://github.com/openwrt/luci/blob/cd06e708d8d8037486b77dab3389ca91a88f2dd0/modules/luci-base/htdocs/luci-static/resources/validation.js#L227
uci/shell-script validation datatypes: https://git.openwrt.org/?p=project/ubox.git;a=blob;f=validate/validate.c;h=e72b8117ecd8b680778b0f5c7637ed6546a7736b;hb=4c7b720b9c63b826fb9404e454ae54f2ef5649d5#l893

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.