This page explain you how can you handle event on server side with SartajPHP Application. There are different types of applications in SartajPHP. The BasicApp is good to learn and develop basic web application. Every Application has some inbuilt events but you can always create your custom events in mostly application. But please remember Native Mobile app or Serverless applications doesn't support server side events. But our focus is here only for web development and we use BasicApp to learn. View Demo here http://www.sartajphp.com/hello.html
Application Event Handling:-
Create an Application (hello.php) with Below Code--
class index extends \Sphp\tools\BasicApp{
private $temp1 = null;
// onstart event trigger on application start processing
public function onstart(){
global $masterf;
// check user permission
$this->getAuthenticate("ADMIN,GUEST");
// set default DB table for components if application use Database Table
$this->setTableName("users");
// create temp object to control and manage HTML output
$this->temp1 = new \Sphp\tools\TempFile($this->mypath . "/forms/email.front");
// set master file for temp object, use if application output is html. Here we use global variable in comp file
$this->setMasterFile($masterf);
}
// new event handler
public function page_new(){
print 'Call My Event This is inbuilt new event';
// stop temp file and master design output, by defualt in new event it is set to show
$this->showNotTempFile();
//for send temp output use $this->setTempFile($this->temp1)
}
public function page_event_myevent1($evtp){
print 'Call New Event This is custom my event';
}
}
View Demo here http://www.sartajphp.com/hello.html
Life Cycle Events of Controls:-
Component has some special event to manage the output. You can handle these events in App. Thease events can bind in tempfile with special attributes and with any app type. WebApp can also bind custom component events automatically but BasicApp don't support this. Here is an example of TextBox component. In temp File you can use on-init, on-create, on-startrender and on-endrender life cycle events. Use these attributes on component tag in temp file.
<input id="aname" type="text" dtable="users" dfield="fname" runat="server" funsetForm="form2" on-create="true" />
on-create="true" will bind event to application and will find function if module app and call method if Class App like BasicApp.
<?php
function comp_aname_on_create($e){
// get component object
$compobj = $e["element"]->getComponent();
// element is sphp html node
$e->element->class = "border";
}
In case of Basic App create public function in class to handle event.
Bind and Handle Custom Component Event:-
In Control class register your custom event like:- $this->registerEvent('onrender');
Now in App you can register handler and handle this event like:-
$this->mycomp->setEventHandler('onrender',"myhandler",$this);
<?php
class index extends \Sphp\tools\BasicApp{
private $home_front1 = null;
public function onstart() {
global $masterf;
$this->setTableName("tbl1");
$this->home_front1 = new \Sphp\tools\TempFile($this->mypath . "/forms/index_front1.front");
// use global variable in comp.php file
$this->setMasterFile($masterf);
// register event handler
$this->home_front1->setEventHandler('onrender',"myhandler",$this);
}
public function myhandler($args){
// args may vary according to caller
echo $args["obj"]->tagName;
}
}
WebApp is a different type of application. It is used design to backcode approch to develop app. BasicApp is more famous where we create app as backcode to front code. So don't worry you can do everything with just BasicApp also. In WebApp you can bind an event handler with just add a method in App like:-
<?php
class index extends WebApp{
public function comp_event_aname_event1($e){
}
}
Create your own Custom Event:-
You can create your own custom event and handle it with use of \Sphp\kit\Event class. In this way different application can communicate each other. browser can also trigger event. So it is on developer.
<?php
// this is component app
class GUIEditorApp extends Sphp\tools\CompApp{
public $evtFileSave = null;
private $tmpobj = null;
public function onstart() {
$this->evtFileSave = new \Sphp\kit\Event($this);
$this->tmpobj = $this->createTempFile($this->mypath . '/apps/forms/GUIEditor.temp',true);
$this->setTempFile($this->tmpobj);
}
public page_event_test($evtp){
// trigger event
$this->evtFileSave->raiseEvent();
}
}
// this is main BasicApp
class index extends \Sphp\tools\BasicApp{
private $tempfile = null;
public $sublayout = "";
public function onstart() {
SphpBase::JQuery()->getJQKit();
$this->tempfile = new \Sphp\tools\TempFile($this->mypath . "/forms/index.temp");
}
// guiedt component group and it has CompApp and that comp app has init event handler.
// so every CompApp has inbuilt init event which you can handle always in parentapp of tempfile
public function guiedt_event_init($compApp) {
$compApp->evtFileSave->setHandler($this,"onFileSave");
}
public function onFileSave($param) {
$this->JSServer->addJSONJSBlock("debugApp();");
}
}