Skip to content
This repository has been archived by the owner on Feb 6, 2021. It is now read-only.

Commit

Permalink
Adds demo of working with responses
Browse files Browse the repository at this point in the history
  • Loading branch information
devraj committed Apr 3, 2015
1 parent 133846d commit 5dc3b0a
Showing 1 changed file with 58 additions and 29 deletions.
87 changes: 58 additions & 29 deletions source/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ Prestans then provides the following additional methods:
Attribute Change Events
-----------------------

Models generated by Prestans raise the ``prestans.types.Model.EventType.ATTRIBUTE_CHANGED`` event whenever a mutator is fired. You can listen for this event on any instance of a Prestans Model subclass:
Models generated by Prestans raise the ``prestans.types.Model.EventType.ATTRIBUTE_CHANGED`` event whenever a mutator is fired. You can listen for this event on any instance of a Prestans Model subclass.

``event.getIdentifier()`` provides you a camel cased representation of the attribute that changed:

.. code-block:: javascript
Expand All @@ -175,7 +177,6 @@ Models generated by Prestans raise the ``prestans.types.Model.EventType.ATTRIBUT
});
``event.getIdentifier()`` provides you a camel cased representation of the attribute that changed.
Generating Model Code
---------------------
Expand Down Expand Up @@ -244,6 +245,7 @@ Our request manager can work this, this is done by using a shared instance of th
})
};
.. note:: Minification support is built into our Xhr client. All you have to do is set the optional parameter to ``true`` and the client negotiates with the server.

Composing a Request
-------------------
Expand Down Expand Up @@ -271,29 +273,6 @@ To place an Xhr request you compose a request by instantiating a ``prestans.rest
* ``prestans.net.HttpMethod.DELETE``
* ``prestans.net.HttpMethod.PATCH``

Placing a Request
-----------------

Then use the ``makeRequest`` method on the Request Manager instance to dispatch API calls, it requires the following parameters:

* ``request`` is a ``prestans.rest.json.Request`` object.
* ``callbackSuccessMethod`` which is a reference to a function the Request Manager calls if the API call succeeds, the method will be passed a response object. Ensure you use ``goog.bind`` to bind your function to your namespace.
* ``callbackFailureMethod`` optional reference to a function the Request Manager calls if the API call fails, this method will be passed a response object with failure information.
* ``opt_abortPreviousRequests``, asks the Request Manager to cancel all pending requests.

.. code-block:: javascript
# Assume you have a request object
pdemo.GLOBALS.API_CLIENT.makeRequest(
request,
goog.bind(this.successCallback_, this),
goog.bind(this.failureCallback_, this),
false
);
.. note:: Request objects tell the manager if they are willing to be aborted, this is configurable per request lodged with the manager.

The second method the Request Manager provides is ``abortAllPendingRequests``, this accepts no parameters and is responsible for aborting any currently queued connections. The failure callback is not fired when requests are aborted.

Example of a GET request which optionally passes two parameters and expects the server to return a set of ``Album`` entities:

Expand All @@ -318,10 +297,17 @@ Example of a GET request which optionally passes two parameters and expects the
]
};
var request = prestans.rest.json.Request(config_);
Example of a GET request which fetches a particular entity:

.. code-block:: javascript
// Optional filter to turn off all attributes bar Album Id and Name
var opt_filter = new pdemo.data.filter.Album(false);
opt_filter.enableName();
opt_filter.enableAlbumId();
var config_ = {
identifier: "AlbumFetch",
httpMethod: prestans.net.HttpMethod.GET,
Expand All @@ -332,17 +318,60 @@ Example of a GET request which fetches a particular entity:
urlArgs: [albumId]
};
var request = prestans.rest.json.Request(config_);
Dispatching a Request
---------------------

Reading a Response
------------------
Once you have a request object use the ``dispatchRequest`` method on the Request Manager instance to dispatch API calls, it requires the following parameters:

* ``request`` is a ``prestans.rest.json.Request`` object.
* ``callbackSuccessMethod`` which is a reference to a function the Request Manager calls if the API call succeeds, the method will be passed a response object. Ensure you use ``goog.bind`` to bind your function to your namespace.
* ``callbackFailureMethod`` optional reference to a function the Request Manager calls if the API call fails, this method will be passed a response object with failure information.
* ``opt_abortPreviousRequests``, optionally asks the Request Manager to cancel all pending requests.

.. code-block:: javascript
# Assume you have a request object
pdemo.GLOBALS.API_CLIENT.dispatchRequest(
request,
goog.bind(this.successCallback_, this),
goog.bind(this.failureCallback_, this),
false
);
.. note:: Request objects tell the manager if they are willing to be aborted, this is configurable per request lodged with the manager.

The second method the Request Manager provides is ``abortAllPendingRequests``, this accepts no parameters and is responsible for aborting any currently queued connections. The failure callback is not fired when requests are aborted.

Working with Responses
----------------------

The Xhr calls back the nominated functions on success and failure. Prestans passes an instance of ``prestans.rest.json.Response`` which has access to th following:

* ``requestIdentifier`` The string identifier for the request type,
* ``statusCode`` HTTP status code,
* ``responseModel`` Class used to unpack response body,
* ``arrayElementTemplate`` prestans.types.Model,
* ``arrayElementTemplate`` prestans.types.Model subclass
* ``responseModelElementTemplates``
* ``responseBody`` JSON Object (Optional)
* ``responseBody`` JSON Object (Optional), this won't be available for failures

In case of a successful request you will be able to retrieve the parsed Prestans JavaScript model using the ``getUnpackedBody()`` method.

.. code-block:: javascript
/**
* @private
*/
pdemo.ui.album.Renderer.prototype.successCallback_ = function(response) {
// response.getUnpackedBody() will return a parsed Prestans model
// based on the rules you defined in the request
console.log(response.getUnpackedBody());
};
In case of failures you get access to the response which has the status code and the original body (if any) of the response. For failures you are expected to do what your application deems appropriate.

Xhr Communication Events
------------------------
Expand Down

0 comments on commit 5dc3b0a

Please sign in to comment.