Fork me on GitHub

The Request Factory


The Jaxon\Request\Factory\RequestFactory class allows to create requests to functions or methods exported with Jaxon. The Jaxon\rq() global function returns an instance of this class, which provides the call() method to create a request. It will often be used together with the Jaxon\pm() function, which is needed to set its parameters.

For example, the following code uses the Request Factory to create a request to a the setColor() method of the class HelloWorld, passing the value selected in the combobox with id colorselect as parameter.

<div class="col-md-4 margin-vert-10">
    <select id="colorselect" name="colorselect"
            onchange="<?php echo Jaxon\rq('HelloWorld')->call('setColor', Jaxon\pm()->select('colorselect')) ?>">
        <option value="black" selected="selected">Black</option>
        <option value="red">Red</option>
        <option value="green">Green</option>
        <option value="blue">Blue</option>
    </select>
</div>

The Request Factory can also be used to bind a call to a Jaxon function to an event.

use function Jaxon\pm;
use function Jaxon\rq;

public function myFunction()
{
    $response->setEvent('colorselect', 'onchange', rq('HelloWorld')->call('setColor', pm()->select('colorselect')));
    return $response;
}

The configured prefix is automatically prepended to the generated javascript call.

Conditional calls

The Request Factory provides 3 functions to check a condition before sending a Jaxon request.

The function when() sends the request only if the given condition is true. In the following example, the request is sent only if the checkbox with id accepted is checked.

use function Jaxon\pm;
use function Jaxon\rq;

public function myFunction()
{
    $request = rq('HelloWorld')->call('setColor', pm()->select('colorselect'))
        ->when(pm()->checked('accepted'));
    $response->setEvent('colorselect', 'onchange', $request);
    return $response;
}

The function unless() sends the request only if the given condition is not true. In the following example, the request is sent only if the checkbox with id refused is not checked.

use function Jaxon\pm;
use function Jaxon\rq;

public function myFunction()
{
    $request = rq('HelloWorld')->call('setColor', pm()->select('colorselect'))
        ->unless(pm()->checked('refused'));
    $response->setEvent('colorselect', 'onchange', $request);
    return $response;
}

The function confirm() asks a question and sends the request only if the user answers yes.

use function Jaxon\pm;
use function Jaxon\rq;

public function myFunction()
{
    $request = rq('HelloWorld')->call('setColor', pm()->select('colorselect'))
        ->confirm('Are you sure?');
    $response->setEvent('colorselect', 'onchange', $request);
    return $response;
}

The content from the webpage can be inserted in the question, by giving their position between braces.

use function Jaxon\pm;
use function Jaxon\rq;

public function myFunction()
{
    $request = rq('HelloWorld')->call('setColor', pm()->select('colorselect'))
        ->confirm('You want {1}? Really, {2}?', pm()->select('colorselect'), pm()->html('username'));
    $response->setEvent('colorselect', 'onchange', $request);
    return $response;
}

The order of the parameters in the message can be changed, allowing for example to implement message translations.

use function Jaxon\pm;
use function Jaxon\rq;

public function myFunction()
{
    $request = rq('HelloWorld')->call('setColor', pm()->select('colorselect'))
        ->confirm('Hey {2}, you really want {1}?', pm()->select('colorselect'), pm()->html('username'));
    $response->setEvent('colorselect', 'onchange', $request);
    return $response;
}

By default, the confirmation message is printed using the javascript confirm() function. The jaxon-dialogs plugin allows to ask the confirmation question using third party javascript libraries.