DOCS

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.

Client

The client object allows your backend to interact with your application's frond-end running in the users browser.

<?php
	$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

<?php
$client->session($key, $value);

Getting the value of a key

<?php
$value = $client->session($key);

Testing if a key exists

<?php
$ok = $client->sessionHasKey($key);

Removing a key

<?php
$client->sessionRemove($key);

Publish a message or event

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

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

Adding a javascript file

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

Adding a css file

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

Adding a javascript controller file

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

Set application data

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

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:

<?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);
	});

Run javascript code

<?php
$jsCode = 'alert("Hello Jose");';
$client->runCode($jsCode);

Run javascript when the application is ready

<?php
$jsCode = 'alert("Hello Jose");';
$client->ready($jsCode);

Related Topics

Session and data persistence

Fork me on GitHub