Using cWebHttpHandler with JSON instead of tWebValueTree

From DataFlex Wiki
Revision as of 23:13, 10 July 2023 by Wil (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

From this forum post: https://support.dataaccess.com/Forums/showthread.php?68231-ToDo-Describe-call-mode-and-a-few-more-questions&p=371095#post371095

Reposting it here so I can quickly find it next time I need it (sorry, not sorry)

It is indeed true that for most situations the tWebValueTree is not needed any more. ClientActions and ServerActions can now use JSON Handles or structs & arrays that are directly serialized. So when calling a server action you can now do the following:

//  Create data objects    
var oData =  {
    labels : ["January","February","March","April","May","June","July"],
    datasets : [{
        strokeColor : "rgba(220,220,220,1)",
        data : [65,59,90,81,56,55,40]
    },{
        strokeColor : "rgba(151,187,205,1)",
        data : [28,48,40,19,96,27,100]
    }]
};

//  Send call to the server
this.serverAction("ProcessData2", [ 2, 1 ], oData, function(oEvent){
    alert(oEvent.sReturnValue);
});
//  Function called as server action
Function ProcessData2 Integer iParam1 Integer iParam2 Returns String
    tActionData tData
    Handle hoJson
    
    //  Retrieve and deserialize data
    Get phoActionJsonData to hoJson
    Get JsonToDataType of hoJson to tData
    
    Function_Return tData.labels[3]
End_Function

So instead of ptActionData you use phoActionJsonData which gives a cJsonObject representing the data. Note that you do not have to cleanup this JSON Handle.

For client actions you can do the following:

Procedure ProcessDataOnClient2
    //  Send the client action
    String[] aParams
    tActionData tData
     
    //  Generate some data
    Move "Januari"      to tData.labels[0]
    Move "Februari"     to tData.labels[1]
    Move "#FF0000"      to tData.datasets[0].strokeColor
    Move 55             to tData.datasets[0].data[0]
    Move 88             to tData.datasets[0].data[0]
     
    //  Call the client action
    Send ClientAction "processData2" aParams tData
End_Procedure


//  Client action called with action data
processData2 : function(){
    var tData;
    
    // Retrieve value tree and deserialize
    tData = this._tActionData;
    
    // Do something with the data
    alert(tData.datasets[0].strokeColor);
}

Note that ClientAction takes a variant parameter that it serializes to JSON. Both API's are backwards compatible so working with tWebValueTree still works. The value tree's are also still used for client web properties.