La section app.functions
de la configuration contient un tableau de fonctions à exporter.
En voici un exemple.
function hello_world($isCaps)
{
$response = jaxon()->newResponse();
$text = ($isCaps) ? 'HELLO WORLD!' : 'Hello World!';
$response->assign('div2', 'innerHTML', $text);
}
'app' => [
'functions' => [
'hello_world',
'sayhello',
],
],
use Jaxon\Jaxon;
jaxon()->register(Jaxon::CALLABLE_FUNCTION, "hello_world");
Après avoir été enregistrée, cette fonction peut être appelée en javascript avec le nom hello_world()
.
Voici le code javascript généré par Jaxon, et envoyé dans le navigateur.
hello_world = function() {
return jaxon.request(
{ jxnfun: 'helloWorld' }, { parameters: arguments }
);
};
Le nom de la fonction javascript peut être changé avec un alias.
use Jaxon\Jaxon;
jaxon()->register(Jaxon::CALLABLE_FUNCTION, "hello_world", ["alias" => "sayHello"]);
Des options peuvent être ajoutées aux fonctions.
'app' => [
'functions' => [
'hello_world' => [
'mode' => "'asynchronous'",
],
],
],
Une méthode d'une classe peut aussi être enregistrée comme une fonction.
Pour cela, le nom de la classe doit être passé à la fonction register()
, comme dans l'exemple suivant.
class HelloWorld
{
public function hello_world($isCaps)
{
$response = jaxon()->newResponse();
$text = ($isCaps) ? 'HELLO WORLD!' : 'Hello World!';
$response->assign('div2', 'innerHTML', $text);
}
}
use Jaxon\Jaxon;
jaxon()->register(Jaxon::CALLABLE_FUNCTION, "hello_world", ["class" => HelloWorld::class]);
De même, un alias peut être défini.
use Jaxon\Jaxon;
jaxon()->register(Jaxon::CALLABLE_FUNCTION, "hello_world", ["class" => HelloWorld::class, "alias" => "sayHello"]);