Using cWebHttpHandler with JSON instead of tWebValueTree: Difference between revisions

From DataFlex Wiki
Jump to navigationJump to search
Created page with "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 th..."
 
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 4: Line 4:


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:
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:
Code:


<source lang="javascript">
//  Create data objects     
//  Create data objects     
var oData =  {
var oData =  {
Line 22: Line 22:
     alert(oEvent.sReturnValue);
     alert(oEvent.sReturnValue);
});
});
</source>


Code:
<source lang="dataflex">
 
//  Function called as server action
//  Function called as server action
Function ProcessData2 Integer iParam1 Integer iParam2 Returns String
Function ProcessData2 Integer iParam1 Integer iParam2 Returns String
Line 36: Line 36:
     Function_Return tData.labels[3]
     Function_Return tData.labels[3]
End_Function
End_Function
</source>


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.
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:
For client actions you can do the following:
Code:


<source lang="dataflex">
Procedure ProcessDataOnClient2
Procedure ProcessDataOnClient2
     //  Send the client action
     //  Send the client action
Line 57: Line 58:
     Send ClientAction "processData2" aParams tData
     Send ClientAction "processData2" aParams tData
End_Procedure
End_Procedure
</source>


Code:


<source lang="javascript">
//  Client action called with action data
//  Client action called with action data
processData2 : function(){
processData2 : function(){
Line 70: Line 72:
     alert(tData.datasets[0].strokeColor);
     alert(tData.datasets[0].strokeColor);
}
}
</source>


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.
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.
[[Category: Web Applications]]

Latest revision as of 23:13, 10 July 2023

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.