EXC | DEV | Documentation
This topic applies to the BackEnd framework.
Data and parameters received on a request are available using the request object.
The client object allows your backend to interact with your application's frond-end running in the users browser.
<?php
$client = \exc\client::instance();
You can use the $client
to make common interactions with your client easily.
Note: Most methods of $client
are chainable. Examples here do not use chaining for sake of simplicity.
A shared session is a set of key-value pairs that persists between request on the server side and are shared automatically with your front-end so you can use them in javascript.
For security reasons the server always overrides the session values on the client.
<?php
$client->session($key, $value);
<?php
$value = $client->session($key);
<?php
$ok = $client->sessionHasKey($key);
<?php
$client->sessionRemove($key);
Similar to controllers on the backend, you can publish a message to your javascript controllers.
<?php
$client->publish("displayRecord", ["id"=>20]);
<?php
$client->addJSFile($aPath);
$client->addJSFile('asset://js/myfile.js'); //may use exc's special url protocols
<?php
$client->addCSSFile($aPath);
$client->addCSSFile('asset://css/mystyle.css'); //may use exc's special url protocols
<?php
$client->addController($objectName, $aPath);
$client->addController('recordController', 'asset://js/controller.record.js'); //may use exc's special url protocols
<?php
$aValue = ["id"=>25, "name"=>"Jose", "lname"=>"Cuevas"];
$client->setData("record", $aValue);
Data is available in your front-end using the propertyapp.data
, for exampleapp.data.record.name
.
When the front-end sends a request that expects data back we use $client->sendResponseData($data)
to send the data. Lets see a very simple example:
<?php
class appController extends \exc\controller\viewController {
public function onAction_ComputeRate(){
$client = \exc\client::instance();
$qty = $client->values['qty'];
$results = ["status"=>200, "cost"=>25 * $qty, "qty"=>$qty];
$client->sendResponseData($results);
$client->done(); //finish the interaction
}
In our front-end we invoke our service using a backend action.
var action = exc.backend.action("@(app.computeRate)"); //create a backend action
action.params.qty = 2; //set a parameter
action.exec().then(function(data){
alert("The cost is:" + data.cost);
});
<?php
$jsCode = 'alert("Hello Jose");';
$client->runCode($jsCode);
<?php
$jsCode = 'alert("Hello Jose");';
$client->ready($jsCode);