Logic Machine Forum
apps/LP page: cleanest way to execute Lua code on jQuery event? - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: apps/LP page: cleanest way to execute Lua code on jQuery event? (/showthread.php?tid=5172)



apps/LP page: cleanest way to execute Lua code on jQuery event? - rbourgeon - 22.12.2023

Hi,
I have made my first LP page that does stuff dynamically using JavaScript and jQuery, for instance load localbus.js.gz and 
  • use localbus.listen() to automatically refresh KNX object values displayed on the page when a KNX object is written
  • use localbus.write() to write on a KNX object when some event happens on the page

but this JavaScript library "localbus" only allows to listen and to write on the values of the KNX objects. What if want to build a jQuery event handler that does something that would be easy to write in Lua? For instance:
  • enable/disable a script : script.enable, script.disable
  • reload the tags of an object : grp.gettags
  • read / write a file : io.readfile, io.writefile
  • update a scheduler : db:execute("UPDATE scheduler_events SET ... WHERE ...")
    ...

I know that I can embed Lua code inside the LP page (with the <? ?> tags), but this Lua code is executed only when loading the page the first time. How would it be possible to execute Lua code on-demand when some event happens on the web page? Huh


RE: apps/LP page: cleanest way to execute Lua code on jQuery event? - admin - 23.12.2023

Use AJAX ($.get/$.post) to call a different .lp script.


RE: apps/LP page: cleanest way to execute Lua code on jQuery event? - jmir - 26.03.2024

Hi admin,

Can you post an example on how to call a .lp file from a .lp file to execute lua code?

Thanks.


RE: apps/LP page: cleanest way to execute Lua code on jQuery event? - admin - 26.03.2024

Add your common code to a user library instead.