jaxon-php / jaxon-js

The Jaxon javascript library https://www.jaxon-php.org.
BSD 3-Clause "New" or "Revised" License
4 stars 2 forks source link

mode synchronous and jaxonResponse->setReturnValue question #15

Closed seba1rx closed 3 years ago

seba1rx commented 4 years ago

I have a question about mode synchronous

when I declare a function like:

$jaxon->register(Jaxon::CALLABLE_FUNCTION, 'functionThatReturnsUsing_setReturnValue', ['mode' => "'synchronous'"]);

and the function code is:

function functionThatReturnsUsing_setReturnValue(){
    $jaxonResponse = new Response();
    $data= array(
        'isValidated' => TRUE,
        'str_data' => 'some data here',
        'int_data' => 123,
        'flt_data' => 1.23
    );
    $jaxonResponse->setReturnValue($data);
    return $jaxonResponse;
}

but trying to get the $data as a function return value like:

<script>
    var myData = jaxon_functionThatReturnsUsing_setReturnValue();
    console.log(myData);
</script>

I am using jaxon v3, and so far the console.log prints false and examinating the response preview with browser console $data is being returned in jxnrv section of response.

How can I get the $data returned to the myData var as result of my call??

I tried:

<script>jaxon.config.defaultReturnValue = true;</script> but it just turns the console.log result to true

$jaxon->setOption('core.request.mode', "synchronous"); but it does not help to my purpose.

In the closed issues there is a similar question that looks old (2017) I tried the solution posted there but it does not seems to work.

Please help me.

feuzeu commented 3 years ago

Hi,

The synchronous request feature is deprecated in javascript, and thus was removed in the last release, and re-implemented with a queue. https://github.com/jaxon-php/jaxon-js/releases/tag/v3.2.0

You can still set some Jaxon requests as synchronous, but they will be queued, and each will be executed only after the response to the previous is received. The return value is not relevant anymore. You can use the response api to set the value of a javascript variable.

function functionThatSetsDataValue(){
    $jaxonResponse = new Response();
    $data= array(
        'isValidated' => TRUE,
        'str_data' => 'some data here',
        'int_data' => 123,
        'flt_data' => 1.23
    );
    $jaxonResponse->call('setData', $data);
    $jaxonResponse->script('console.log(myData)');
    return $jaxonResponse;
}
<script>
    var myData = null;
    function setData(data) {
        myData = data;
    }
    jaxon_functionThatSetsDataValue();
</script>
seba1rx commented 3 years ago

Hi,

The synchronous request feature is deprecated in javascript, and thus was removed in the last release, and re-implemented with a queue. https://github.com/jaxon-php/jaxon-js/releases/tag/v3.2.0

You can still set some Jaxon requests as synchronous, but they will be queued, and each will be executed only after the response to the previous is received. The return value is not relevant anymore. You can use the response api to set the value of a javascript variable.

function functionThatSetsDataValue(){
    $jaxonResponse = new Response();
    $data= array(
        'isValidated' => TRUE,
        'str_data' => 'some data here',
        'int_data' => 123,
        'flt_data' => 1.23
    );
    $jaxonResponse->call('setData', $data);
    $jaxonResponse->script('console.log(myData)');
    return $jaxonResponse;
}
<script>
    var myData = null;
    function setData(data) {
        myData = data;
    }
    jaxon_functionThatSetsDataValue();
</script>

tahnk you so much for the explanation, sorry for not having seeing the release description