Skip to content

Latest commit

 

History

History
92 lines (69 loc) · 2.31 KB

doc_client_backend.md

File metadata and controls

92 lines (69 loc) · 2.31 KB

Documentation Version 0.1.0

Topics

Interacting with the backend

In the client side of your application we use the functionality provided by exc.app.backend to interact with your backend.

The basic way to interact with the backend is by means of actions.

RELATED
Interacting with the client

Create an action

var a = exc.backend.action("@(main.test)");

In EXC the simplest way to interact is using an action selector. In our example we use @(main.test).

An action selector is indicated by the "@()". The arguments are the name of a controller followed by the action we want to send to the controller.

In our case the controller is "main" and the action is "test".

An action may include optional parameters that are send as traditional form data using HTTP POST.

a.params.name = "Jose";
a.params.qty = 5;

We use the function exc.app.backend.sendAction() to send our action to the backend.

exc.app.backend.sendAction(a);

We can include data to our backend using the data argument of sendAction.

var data = {
	name = "Jose",
	qty = 5,
	zipcode="00603"
};
exc.app.backend.sendAction(a, data);

You can specify a success callback and a failure callback.

var data = {
	name = "Jose",
	qty = 5,
	zipcode="00603"
};

exc.app.backend.sendAction(a, data, function(state){
	//success
	var shippingCost = state.data.cost;
},
function(){
	//failure
	alert("Unable to compute shipping costs...");
},
);

The function sendAction() returns a promise.

var promise = exc.app.backend.sendAction(a, data);

Use the then method of the promise to set a a callback to execute when action is completed.

exc.app.backend.sendAction(a, data).then(function(state){
	//success
	var shippingCost = state.data.cost;
});

With then you can simply specify the name of an event that you want to be triggered.

exc.app.backend.sendAction(a, data).then("eventToPublish");

You may also specify a method of a controller object to execute.

exc.app.backend.sendAction(a, data).then("@(myController.updateCosts)");

See Interacting with the client for details on how to handle actions send to the backend.