EXC | DEV | Documentation

Interacting with the front-end

This topic applies to the BackEnd framework.

Getting data

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.

	$client = \exc\client::instance();

Interactions with your front-end

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.

The shared session

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.

Setting a key

$client->session($key, $value);

Getting the value of a key

$value = $client->session($key);

Testing if a key exists

$ok = $client->sessionHasKey($key);

Removing a key


Publish a message or event

Similar to controllers on the backend, you can publish a message to your javascript controllers.

$client->publish("displayRecord", ["id"=>20]);

Adding a javascript file

$client->addJSFile('asset://js/myfile.js'); //may use exc's special url protocols

Adding a css file

$client->addCSSFile('asset://css/mystyle.css'); //may use exc's special url protocols

Adding a javascript controller file

$client->addController($objectName, $aPath);
$client->addController('recordController', 'asset://js/controller.record.js'); //may use exc's special url protocols

Set application data

$aValue = ["id"=>25, "name"=>"Jose", "lname"=>"Cuevas"];
$client->setData("record", $aValue);
Data is available in your front-end using the property, for example

Respond to a request

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:

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->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

		alert("The cost is:" + data.cost);

Run javascript code

$jsCode = 'alert("Hello Jose");';

Run javascript when the application is ready

$jsCode = 'alert("Hello Jose");';

Related Topics

Session and data persistence

Fork me on GitHub