Skip to content

What does the Control layer do?

rishson edited this page Mar 22, 2012 · 1 revision

For widgets that want to make a request for data, they don't even need to know about XHR, Transports or the control layer. Widgets can simply publish their request (along with a topic that the response will be published on):

//dojo 1.7+ example of calling a RestService on the server and expecting a response to be published to a topic
 var restCall = new RestRequest({url : 'testService',
    verb : 'delete',
    params : [{exampleParamsName : 'exampleParamValue'}],
    topic : 'testServiceResponse');

topic.publish(Globals.SEND_REQUEST, restCall);

You can make a call to the server without having to directly use the lower level XHR code. Since the actual mechanism for getting to the server and parsing the response are abstracted, you can have a clean separation of concerns between the widgets and the control layer. The control layer knows nothing of the mechanics of performing a request; this is delegated to a Transport implementation that is injected into the control layer on construction.

e.g.

//example of calling a WebService on the server and expecting a response on a callaback function
 var serviceCall = new ServiceRequest({service : 'testService',
    method : 'TestMethod',
    params : [{exampleParamsName : 'exampleParamValue'}],
    callback : myCallback,
    scope : this});

 controller.send(serviceCall);

//example of calling a RestService on the server and expecting a response to be published to a topic
 var restCall = new RestRequest({service : 'testService',
    verb : 'post',
    params : [{exampleParamsName : 'exampleParamValue'}],
    topic : 'testServiceResponse');

 controller.send(restCall);

Because of this abstraction, we can plug different Transport implementations into our control layer. Provided out of the box are XhrTransport (for performing Xhr post calls) and MockTransport (for use in a headless unit test configuration with no need for a running web server).

Web Service requests and Rest requests are currently supported. For rest, get, delete, post and put are supported.

Responses

The server response is wrapped in a Response object that provides a standard set of response types regardless of the type of request that was made to the server.

dojo.subscribe('/some/topic/passed/to/control/layer', this, function(response) {
  if(response.isOk) {
    //do something with the response
    console.debug(response.payload);
  }  
  else if(response.isInvalid) {
    //our request must have been invalid
  }
  else if(response.isConflicted) {
    //basically a concurrent modification exception
    console.debug(response.payload);  //should be the latest copy of the resource we tried to mutate
  }
});
Clone this wiki locally