The app.functions
section of the configuration contains an array of functions to be registered.
Here's an example.
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");
After it is registered, the function can be called from javascript with the name hello_world()
.
Here's the code that Jaxon will generate and send to the browser.
hello_world = function() {
return jaxon.request(
{ jxnfun: 'helloWorld' }, { parameters: arguments }
);
};
The javascript function name can be changed to an alias.
use Jaxon\Jaxon;
jaxon()->register(Jaxon::CALLABLE_FUNCTION, "hello_world", ["alias" => "sayHello"]);
Options can be set on functions.
'app' => [
'functions' => [
'hello_world' => [
'mode' => "'asynchronous'",
],
],
],
A method of a class can also be registered as a function.
In this case, the class name is passed to the register()
call, as in the following example.
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]);
An alias can also be defined.
use Jaxon\Jaxon;
jaxon()->register(Jaxon::CALLABLE_FUNCTION, "hello_world", ["class" => HelloWorld::class, "alias" => "sayHello"]);