Emitters

Jybrid has Event/Emitter methods, they triggers on defined positions during an Request-Response Cycle. This Events are NOT DOM events.

In Xajax, the emitters had to be integrated directly via Javascript. Jybrid allows these Javascript functions to be integrated directly via PHP. Several emitters can be added to an event simultaneously. This construction follows the concept that Javascript can be controlled with simple PHP functions as far as possible without having to be completely clear about the spelling and anchoring of the individual Javascripts every time. This is largely automatic.

You can set Emitter global in jybrid. You can set Emitters per request. You can combine core an request emitters. You can also let it be ;-)

Event-Name description core-event per-request event
onRequest Directly during the jybrid.submitRequest x x
onSuccess Response http code was an success code (200). It does not tell something about processing the response x x
onBeforeChallengeRequest before the request will be send again but now with credentials x x
onBeforeResponseProcessing after an Response Successfully received the jybrid response processor will start to handle all response-parts such as jyb.html() x x
onComplete All response commands was processed [oRequest] x x
onFailure Response http code was an alert code between 400 and 599 [(int)'error-code-from-server',oRequest] x x
onFailureResponse The Server responses an not during jybrid handlebar error x x
onFailureRequest Jybrid has catch'd an error during the send method['e error-object'] x x
onResponseDelay Jybrid script can be noticed, that the response is delayed [oRequest],responseDelayTime x x
onExpiration the jybrid script can be noticed that it dont want wait anymore for the response [oRequest],expirationTime x x

Global Jybrid Emitter

You can create a set of different emitters. These emitters are then responsible for all requests. This is especially good for "spinners" and other request status messages that the user of the website should see. Here you can also catch bugs 400 and 500.


// Emitter as body only
$onRequestEmitter = <<<EOT
    jybrid.html('protocol','<p>onRequest: Request is started</p>');
    jybrid.$('waiting').style.visibility = 'visible';
EOT;

// Emitter as complete function 
$onCompleteEmitter = <<<EOT
function() {
    jybrid.prependNode('protocol','<p>onComplete :: Response was completely processed<br/></p>');
    jybrid.$('waiting').style.visibility = 'hidden';
}
EOT;

// You can also add later an emitter to the special event
$onRequestForgottenEmitter = <<<EOT
    jybrid.html('protocol','<p>onRequest: I had have forgotten my Emitter but set him later ;-)</p>');
EOT;

// Adding the emitter content 
Factory::getEmitters()
       ->addEmitter('onRequest', "function(){{$onRequestEmitter}}")
       ->addEmitter('onComplete', "{$onCompleteEmitter}");
       ->addEmitter('onRequest', "function(){{$onRequestForgottenEmitter}}")

....
....
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Schematic Emitter | jybrid Tests</title>
    <?php echo \Jybrid\Scripts\Generator::generateClientScript(); ?>
</head>

Per Request Emitter

Maybe you have requests that need an extra emitter. For example, for uploads or other things. Then you can set each request individually and store the appropriate emitters for this request only.

$testFormExpirePerRequest = \Jybrid\Factory::register('testFormExpirePerRequest');

/** Delayed */
$onResponseDelayEmitterPerRequest = <<<EOT
function(){
jybrid.prependNode('protocol','<p style="color: greenyellow">onResponseDelay Response was delayed '+jybrid.config('responseDelayTime') / 1000+'seconds</p>');
}
EOT;

// you can set the per request emitters before jybrid global emitters
$testFormExpirePerRequest->getEmitters()  ->addEmitter('onResponseDelay', $onResponseDelayEmitterPerRequest)
    // tell jybrid that you dont want use the global onResponseDelay for this request.
                         ->setIgnoreSignal('onResponseDelay');

In the example above you can see that the emitter generation is the same as in the jybrid global emitters, but is only applicable to this request.

setIgnoreSignal ('string')

This method is relatively simple to explain. Hereby you instruct jybrid, if an "onResponseDelay" emitter is set globally in jybrid, to ignore it and to use the request's own emitter exclusively for this request. This ignore signal only affects this request. The ignore signal does not remove any "onResponseDelay" emitter present in jybrid."

setGlobalEmittersIgnore(['array'])

$testForm2perRequestEmitterIgnoreAllGlobalEmitters = \Jybrid\Factory::register('testForm2PerRequest', ['method' => 'GET']);
$testForm2perRequestEmitterIgnoreAllGlobalEmitters->getEmitters()->setGlobalEmittersIgnore(Factory::getEmitters()->getEmitterNames());

In this example you get all set global-emitter names and set these emitters to ignore. All emitters set up to this call are taken into account.

further reading ajax examples

@see schematic-emitter.php

@see schematic-emitter-per-request.php