Extend TMS WEB Core with JS Libraries with Andrew: Epic JSON Primer (part 1)
For the rest of this document, we’re going to be doing triple the work – showing the JavaScript version (JS), the TMS WEB Core wrapper version (WC), and the Delphi version (PAS) of each scenario, presented as a block of code suitable for inclusion (and tested!) within a TMS WEB Core project, which supports all three of these seamlessly. If you were working on a part of a project that used only Delphi VCL code, you could use just the PAS code equally well there. Or if you were working on a part of a project that used pure JavaScript, then the JS code within the asm … end blocks would work just as well in that environment. We’ll even cover a little bit about moving between these three environments. To begin with, we’ll need to know how to define variables to reference JSON objects. We’ll also need to know how to supply JSON to them (sometimes referred to as serialization), and how to see what they contain (de serialization). For the sake of simplicity, we’re going to show our work using console.log() which works in both Delphi and JavaScript in our TMS WEB Core environment. Simply substitute with ShowMessage or some other equivalent debugging output if you’re working in any other environment. But why do we have to define variables at all? Well, one of the key differences between JavaScript and Delphi is how data types are managed. Delphi is a strongly-typed language meaning that, at nearly every step, the data type assigned to a variable is known, usually in advance of its being used. And these data types tend to be rather specific in nature. A JSON Object is not necessarily interchangable with a JSON Array, for example. JavaScript, on the other hand, is a weakly-typed (some might even say non-typed) language. Data types are sort of an afterthought and you can write all kinds of code without having to think about what kind of data is flowing through it. There are significant trade-offs to both approaches. JSON is also very closely tied to the JavaScript language itself, and some aspects of the JavaScript language that have evolved over the years have led to significant improvements in using JSON in that environment. The overall result is that the JS code we’ll be showing tends to be very short, sometimes a little cryptic, but often very efficient. The Delphi equivalents often have to do a little more work to achieve the same thing, but not always less efficiently, as we shall see. Using JS code in TMS WEB Core just involves wrapping it in an asm … block. We’re not using any other libraries or supporting code to do our work here. The WC variations similarly will work without anything special in terms of the TMS WEB Core environment. The classes we’ll be using are there by default, collectively referred to as the TJS* classes. For Delphi though, we do need the extra little step of adding WEBlib.JSON to the uses clause of our project (or Form, etc.). This brings in the TJSON* classes that we’ll be using. Here then is our sample WebButtonClick procedure that we’ll use in nearly every example that follows. It will start with whatever JS, WC or PAS variables we need and then contain […]
