Les confirmations
Cet exemple montre l'utilisation de la fonction confirm et la commande confirm pour exécuter des actions conditionnelles.
When applied to a function call generated with a call factory (see the HTML template), the confirm function will ask the provided question and make the call only if the user confirms the question.
The confirm command in the Response object (see the HelloWorld class) will also ask a question, and execute the commands defined in the provided closure only if the user confirms that question.
The commands processing is halted while waiting for the user confirmation, and resumes after.
So in this example, the text color is changed only after the user answers to the second question.
The HTML code.
<div class="row">
<div class="col-md-12" id="hello-text">
</div>
<div class="col-md-4 select">
<select class="form-select form-control" id="colorselect" name="colorselect" <?= attr()
->on('change', rq('HelloWorld')->setColor(Jaxon\select('colorselect'))
->confirm('Set color to {1}?', Jaxon\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>
<div class="col-md-8 buttons">
<button type="button" class="btn btn-primary" <?= attr()
->click(rq('HelloWorld')->sayHello(1)
->confirm('Convert to uppercase?')) ?>>CLICK ME</button>
<button type="button" class="btn btn-primary" <?= attr()
->click(rq('HelloWorld')->sayHello(0)
->confirm('Convert to lowercase?')) ?>>Click Me</button>
</div>
</div>
The Jaxon setup code.
<?php
class HelloWorld
{
public function sayHello(bool $isCaps)
{
$text = $isCaps ? 'HELLO WORLD!' : 'Hello World!';
$xResponse = jaxon()->getResponse();
$xResponse->assign('hello-text', 'innerHTML', $text);
}
public function setColor(string $sColor)
{
$xResponse = jaxon()->getResponse();
$xResponse->confirm(function($xResp) {
$xResp->sleep(50);
}, 'Sleep for 5 seconds?');
$xResponse->assign('hello-text', 'style.color', $sColor);
}
public function showError($sMessage)
{
$xResponse = jaxon()->getResponse();
$xResponse->assign('hello-text', 'innerHTML', $sMessage);
}
}
// Register object
$jaxon = jaxon();
$jaxon->app()->setup(configFile('class.php'));
$jaxon->setAppOption('dialogs.default.confirm', 'cute');
// Js options
$jaxon->setOption('core.language', 'fr');
$jaxon->setOption('js.lib.uri', '/js');
$jaxon->setOption('js.app.minify', false);
The Javascript code is generated from this PHP template.
jaxon.dom.ready(() => {
// Call the HelloWorld class to populate the div
<?= rq('HelloWorld')->sayHello(0) ?>;
});