[Pas2js] Just sharing the class helpers importance
Michael Van Canneyt
michael at freepascal.org
Sat Dec 22 09:08:13 CET 2018
On Sat, 22 Dec 2018, silvioprog wrote:
> Hi dudes.
>
> Many JavaScript libraries allows to use plugins (or modules) to extend
> their APIs. In general, such plugins just extends their object(s) prototype.
>
> For example, the jsPDF doesn't allows to auto generate tables easily by
> itself, but we can change it using a plugin like this jsPDF-AutoTable plugin
> <https://github.com/simonbengtsson/jsPDF-AutoTable>, and the table
> generation is a piece of cake. ☺ This link
> <https://simonbengtsson.github.io/jsPDF-AutoTable/> contains a lot of new
> features which can be added to make it easy to generates tables in our PDFs.
>
> "Ok guy, but and the class helpers?". 😎 The class helpers will extend our
> class methods, something like extending the JS' prototype. Take a look at
> this small example below:
>
> uses
> jsPDF,
> jsPDF_AutoTable;
>
> var doc := TjsPDF.new('p', 'pt');
> doc.autoTable(columns, rows);
> doc.save('table.pdf');
>
> The method "autoTable()" will be "magically" added into the class "TjsPDF"
> by the class helper "TjsPDFAutoTable" declared in the "jsPDF_AutoTable"
> (plugin) unit. 😃
>
> In that context, the class helpers will fit like a glove!
I agree totally, but I should point out that then we'll have 2 kinds of type
helpers: normal ones (where you write the code) or external ones (where the
code is in a mixin javascript library).
That would require an extra keyword to be added.
So for your example, this would be something like:
TAutoTableHelper = type helper external for class TJSPDF
procedure autoTable(aCols,aRows : Integer);
end;
This way the compiler knows that the type helper does not contain any code.
Another possibility (maybe faster to implement) would be an 'external'
interface, but that would require a typecast to be used, so a type helper will
be easier to use. So currently I think we'll probably do that then.
We'll keep it in mind for when we start with the type helpers.
Thanks for pointing it out !
Michael.
More information about the Pas2js
mailing list