Fork me on GitHub

The Request Factory


The Jaxon\Request\Factory class can be used to create requests to functions or methods exported with Jaxon. The rq() global function returns an instance of this class, which provides the call() method to create a request, and a range of other functions to pass elements from the HTML page as parameter to the request.

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 rq()->call('HelloWorld.setColor', rq()->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 in a Jaxon function to bind a call to a Jaxon function to an event.

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

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

The following methods are used to get content from the webpage.

  • form($sFormId): returns the values of the form with the given id.
  • input($sInputId): returns the value of the input field with the given id.
  • checked($sInputId): returns the value of the checkbox with the given id.
  • select($sInputId): returns the value of the combobox with the given id.
  • html($sElementId): returns the text of the HTML element with the given id.
  • js($sValue): returns a javascript variable or function call.

The full list of functions of the Jaxon\Request\Factory class is documented here.

The Request Factory trait

The Jaxon\Request\Traits\Factory trait adds a call() function to the Jaxon classes, that simplifies the creation of Jaxon requests to their methods. It takes as a parameter the name of a method, and automatically prepends the name of the javascript class.

class MyClass
{
    use \Jaxon\Request\Traits\Factory;

    public function myMethod($color)
    {
        $response->onClick('button-ok', $this->call('myMethod', rq()->select('colorselect')));
        return $response;
    }
}

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.

public function myFunction()
{
    $request = rq()->call('HelloWorld.setColor', rq()->select('colorselect'))
        ->when(rq()->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.

public function myFunction()
{
    $request = rq()->call('HelloWorld.setColor', rq()->select('colorselect'))
        ->unless(rq()->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.

public function myFunction()
{
    $request = rq()->call('HelloWorld.setColor', rq()->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.

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

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

public function myFunction()
{
    $request = rq()->call('HelloWorld.setColor', rq()->select('colorselect'))
        ->confirm('Hey {2}, you really want {1}?', rq()->select('colorselect'), rq()->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.