Smart wrappers, twirk like jQuery
I was asked how to better implement a “JQuery like syntax” – but for Smart Pascal rather than surrendering to alien and cryptic JavaScript. Well, there are many ways to skin a cat, but below is a typical example of how can wrap interesting functionality more or less like jQuery does it; Except within the confines of Smart Pascal.
But naturally, object pascal is a much more rigid language. It has rules and clear principles which cant (and should not) be abused. Also, class helpers is the proper way to extend the functionality of classes through abstraction.
Anyways, here is a way you could do it:
TEventReference = Class(TObject) private FInstance: TW3TagObj; FEventName: String; public procedure &To(EntryPoint:TProcedureRef); constructor Create(Obj:TW3TagObj;EventName:String); end; TControlReference = class(TObject) private FParent: TW3Component; FInstance: TW3TagObj; public function Attach(EventName:String):TEventReference; constructor Create(AParent:TW3Component;AInstance:TW3TagObj); end; TSelector = class abstract public class function &Object(Container:TW3Component;ControlName:String):TControlReference; end; //############################################################################# // TEventReference //############################################################################# constructor TEventReference.Create(Obj:TW3TagObj;EventName:String); begin inherited Create; FInstance := Obj; FEventName := EventName; end; procedure TEventReference.&To(EntryPoint:TProcedureRef); begin w3_AddEvent(FInstance.Handle,FEventName,Entrypoint); end; //############################################################################# // TControlReference //############################################################################# constructor TControlReference.Create(AParent:TW3Component;AInstance:TW3TagObj); begin inherited Create; FParent := AParent; FInstance := AInstance; end; function TControlReference.Attach(EventName:String):TEventReference; begin result := TEventReference.Create(Finstance,EventName); end; //############################################################################# // TSelector //############################################################################# class function TSelector.&Object(Container:TW3Component;ControlName:String):TControlReference; var x: Integer; mObj: TObject; begin result := NIL; if Container<>NIL then begin controlname:=controlname.trim(); if controlname.length>0 then begin mObj := Container.ChildByName(ControlName); if mObj<>NIL then result := TControlReference.Create(Container,TW3Component(mObj)); end; end; end;
What is this good for?
The above code does very little. It is just an example of how you can build chains of functionality using standard object-orientation. In this case all we do is to to:
- Locate a component by name from a container
- Bind to an event by name
- Attach event handler to event
But it may be helpful for others to see how to do this, so I figured it was worth 5 minutes.
The syntax goes like this:
TSelector.Object(Form1,'w3button1').Attach('onclick').To( procedure () begin // your event handler here end);
Note: Since JavaScript has automatic garbage collection you don’t need to worry about creating instances on the fly, they are collected and dropped by the VM as soon as they go out of scope.
To simplify things even more, we can declare TSelector as a global (unit) variable:
var JQuery: TSelector;
And now we can do the same thing, but using the variable name instead:
JQuery.object(Form1,'w3button1').Attach('onclick').To( procedure () begin // your event handler here end);
More advanced stuff
if you want to make use of property and array references, which would be more natural to mimic jQuery and other classic JS libraries – you will have to create an instance and serve that through a function.
var __JQUERY: TSelector; function JQuery:TSelector; begin result := __JQUERY; end; initialization begin __JQUERY := TSelector.Create; end;
With this in place we can add a default property, as such:
TSelector = class(TObject) private function getForm(name:String):TW3CustomForm; public property Forms[name:String]:TW3CustomForm read getForm;default; class function &Object(Container:TW3Component;ControlName:String):TControlReference; end;
And now we can can support a more direct approach (must also alter the sub classes accordingly):
JQuery['form1'].with('w3button1').attach('onclick').to( procedure () begin // your event handler here end);
Leave a Reply Cancel reply
Calendar
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
Recent
The vatican vault
- January 2022
- October 2021
- March 2021
- November 2020
- September 2020
- July 2020
- June 2020
- April 2020
- March 2020
- February 2020
- January 2020
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- February 2013
- August 2012
- June 2012
- May 2012
- April 2012
Something weird is the “abstract” keyword, the compiler does not even emit an error.
Another thing I figured out is the eventName should be “click” instead of “onclick”.
In what context? abstract class, method?
Using similar approach a just created a demo using SMS. I just created a wrapper aound Framework 7 (MIT license). Can I use MIT licence in smart mobile studio application?
see live preview:
https://rawgit.com/smartpascal/smartms/master/smsbasic/www/preview.html