Home > CSS, Delphi, JavaScript, Object Pascal, OP4JS, QTX, Smart Mobile Studio > TQTXReader and TQTXWriter for Smart Mobile Studio

TQTXReader and TQTXWriter for Smart Mobile Studio

February 3, 2015 Leave a comment Go to comments

Storage has become a theme the past couple of weeks for Smart Mobile Studio. I have posted twice about the filesystem classes I have added to QTX, but I have also added more – much more! As you will see over the next weeks 🙂

Whenever you need to write or read data, you really end up with variants in HTML5. Javascript has this thing where everything is either an object (read: prototypical object) an array, or an intrinsic value. To make writing and reading named value pairs easier I have created two classes, cleverly called TQTXReader and TQTXWriter.

Propertybag's are fun!

Propertybag’s are fun!

The writer allows you to, well.. write into a buffer (managed by the class). Like this:

var mObj:=TQTXWriter.Create(null);
try
  mObj.writeStr("first-name",edFirstname.text);
  mobj.writeStr("last-name",edLastName.text);
  mObj.writeStr("cardID",edVisa.text);

  CallCardLookup(mObj.Deserialize, procedure ()
    begin
      showmessage("Visa card is valid");
    end);
finally
  mObj.free;
end;

And TQTXReader does the exact same, but in reverse. The constructor can take a variant containing the information you want to read (stored with TQTXWriter) and extract info in the same way.

Practical uses

Loads! You can use it to tailor custom messages to be used with the spanking new message-api. That way you don’t have to fiddle with JObject based classes. Serialize() and Deserialize() are the methods for turning the data into a string and back again.

You can use it to avoid strictly typed records, which can be handy – especially for grid designs and/or “flexible” arrays of content. But please remember that Serialize() and Deserialize() are costly in terms of speed.

It’s also perfect to interface with JS libraries which expects name-value pairs in constructors and setup routines.

But word to the wise – if all you need is a one-shot wonder, then TQTXWriter is overkill. In such a case you can get away with an anonymous record:

  Fhandle.setupJS( record
     mouse  := "yes";
     startX := 100;
     startY := 100;
   end);
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: