Ben Langhinrichs

Photograph of Ben Langhinrichs

E-mail address - Ben Langhinrichs

February, 2021
 01 02 03 04 05 06
07 08 09 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27

Search the weblog

Genii Weblog

Civility in critiquing the ideas of others is no vice. Rudeness in defending your own ideas is no virtue.

Mon 1 Feb 2021, 05:11 PM
Inline PNG image
After a delay due to various projects and a product release, I wanted to get back to the demo I described in REST plus RPC: Do the demo backward.
If you remember, I showed how to get the data from the view in REST plus RPC: the right data in the right format, and no more. While it would be possible to pull all the data at one time with this demo, it often would not be if there were many more orders or more textual content. Therefore, looking at the basic JavaScript I wrote about in REST plus RPC: building the JavaScript  for RPC, we should assume we might have multiple batches, one for each page of content. By default, that means 20 orders per batch. Since the image above shows that 312 orders are there to be processed, that would be sixteen calls. A developer knowing the data to be returned and the processing to be done might decide to do this in fewer calls or more calls, and thus adjust the page size accordingly.
The JSON-RPC commands all have a particular format with a method required and parameters and return id used as needed. But when the fetch is done, the body of the request is always a single, simple JSON string. So, one of the choices to be made that will vary by project is whether to build up the objects as JavaScript objects and use stringify to create the JSON string, or whether to build the JSON string directly. Sometimes one works better, sometimes the other. To show what I mean, look at the JavaScript object for the rtc.appendTable method that creates the outer tabbed table along with its first tab set to the specified state. We could create all the tabs at once, but we don't know which states are represented, so we need to do them one by one. 
var tbl = {
           jsonrpc: "2.0", 
           method:  "rtc.appendTable", 
           params:  [1, 
                     "TableWidth=FIT_TO_WINDOW RowDisplay=TABBED TableColorStyle=ALTERNATING_ROWS TableColor=RGB255,191,191 TableBodyColor=RGB127,255,255"
This is a template, but in order to set the actual tab of the first state as we create the table, we would modify the object to add an additional parameter for the first "column", which is also the first tab of a tabbed table:
tbl.params.push("Text='' TabLabel='"+ o.State + "'");
After we do this, the object looks like this:
var tbl = {
           jsonrpc: "2.0", 
           method:  "rtc.appendTable", 
           params:  [1, 
                     "TableWidth=FIT_TO_WINDOW RowDisplay=TABBED TableColorStyle=ALTERNATING_ROWS TableColor=RGB255,191,191 TableBodyColor=RGB127,255,255",
                     "Text='' TabLabel='AL'"
If you want to use templates for the calls, they can just be constants, but here is where the downside comes in. In order to use the templates, you have to do a deep copy using either a utility library like lodash, or some clunky code such as:
var rowTmpl = {
               jsonrpc: "2.0", 
               method:  "rtc.appendRow", 
               params:  [false, 1]
var appendRow = JSON.parse(JSON.stringify(rowTmpl));
appendRow.params.push("Text='' TabLabel='"+ o.State + "'");
batch = JSON.stringify(batchActions);
It works, but is inelegant at best, and also does a lot of work given that you are converting the eventual object to JSON anyway. So, an alternative would be to built the template as JSON to start with:
const rowTmplStart = "{\"jsonrpc\":"2.0", \"method\":\"rtc.appendRow\", \"params\":[false, 1, ";]
const rowTmplEnd = "]}";
var appendRow = rowTmplStart+"\"Text='' TabLabel='\""+ o.State + "'\"" + rowTmplEnd;
batch += appendRow;
For this demo, I think we are better off using the JSON string, but I wanted to be clear what I was doing, as it is a little less obvious than building an array of objects and stringifying them. Now, we just have to put it all together. I hope to work on that tonight.

Copyright 2021 Genii Software Ltd.