Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2019-07

pas2js: registering jQuery Mobile event callback

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Simon Ameis

Hello all,

How do I register a Pascal method as callback function for an jQuery Mobile callback?

In JavaScript I can use this syntax:

$("#elementid").on("pagehide", function(event, ui){ alert("Event fired");});

Is there any method to do this in Pascal without creating an external class for the jQuery object $("#elementid")?

Regards
Simon Ameis

Reply by email

Michael Van Canneyt
to Simon Ameis
Simon Ameis wrote 11 lines
Hello all, How do I register a Pascal method as callback function for an jQuery Mobile callback? In JavaScript I can use this syntax: $("#elementid").on("pagehide", function(event, ui){ alert("Event fired");}); Is there any method to do this in Pascal without creating an external class for the jQuery object $("#elementid")?

Several ways, but
TJSOBject(JQuery('#elementid'))['onhide';
should work.

Michael.

Reply by email

Simon Ameis
to Michael Van Canneyt
Michael Van Canneyt wrote
Several ways, but TJSOBject(JQuery('#elementid'))['onhide';  should work.

Thanks for the hint to the libjquery unit. This helps to resolve $('#elementid').

However I couldn't find any information on how to access the non-standard events via attributes. Thus I create an external class just for the "on" method.

Reply by email

Michael Van Canneyt
to Simon Ameis
Simon Ameis wrote 11 lines
Am 17.07.2019 um 23:46 schrieb Michael Van Canneyt: > Several ways, but > TJSOBject(JQuery('#elementid'))['onhide'; >  should work. Thanks for the hint to the libjquery unit. This helps to resolve $('#elementid'). However I couldn't find any information on how to access the non-standard events via attributes. Thus I create an external class just for the "on" method.

See above.

Either you declare an external class, or you typecast to TJSObject (the generic Object from javascript) and use the standard 'properties' array.

There is no other way.

The web unit contains a declaration for most HTML elements. if the events you need are standard HTML elements, the TJSHTMLElement class or one of its descendents are all you need. if the event is a custom event, then you need either an external class or the above.

Given that pascal is supposed to be type safe, I would always go for the external classes approach. It has the additional advantage that the lazarus code completion will create a handler of the correct signature for you.

i.e.
TYourOnject(JQuery('#elementid'));
with the cursor on @mymethod, invoking code completion (CTRL-SHIFT-C) will create the method for you with the correct signature. Saves typing, and you know your handler is correct.

Michael.

Reply by email