Learn with our tutorials and training

developerWorks provides tutorials, articles and other
technical resources to help you grow your development skills
on a wide variety of topics and products. Learn about a specific
product or take a course and get certified. So, what do you want to learn
about?

Featured products

Featured destinations

Find a community and connect

Learn from the experts and share with other developers in one of our
dev centers. Ask questions and get answers with dW answers. Search for local events
in your area. All in developerWorks communities.

JsonRestStore is a good solution for linking Dojo to REST services. By
default, though, JsonRestStore has expectations for the format of the
information passed to, and returned by, the service. The Service Mapping
Description (SMD) facility for customizing these interactions is, at best,
difficult to understand.

JsonRestStore is a data store that provides full read, write, and
notification capabilities through standards based on HTTP/REST interaction
with the server using GET, PUT, POST, and DELETE commands. It lets you
communicate with server-side database/persistent data storage using the
Dojo Data API with JavaScript, and efficiently handles create, read,
update, and delete (CRUD) operations.

When Dojo application authors need to communicate with a preexisting
back-end REST store, when the store's communications cannot be changed,
they use a custom dojox.rpc.Service implementation.

In this article, learn a simple, generic service implementation for wiring
up nonstandard REST services to JsonRestStore. Explore how to use and
extend it for your own services by walking through a practical
example.

To download the source code for this article, see the Download section.

Example service implementation

EasyRestService provides implementations for the
four REST operations: POST (create), GET (read), PUT (update), and DELETE
(delete). For each activity, it provides the ability to hook in before and
after invocation of the service method. The invocation life cycle is:

Call the arguments transformer for the method. This method can alter
the invocation structure (including the path), and can be used to
transform the invocation structure to conform with the REST service's
expectations.

Populate the putData/postData/deleteData/getData node of the
invocation structure by translating the content node to a JSON
representation.

Call the XHR method with the altered XHR invocation structure.

Add a results transformer to the XHR callback. This method can alter
the data structure of the results. Use it to transform the results
into the structure expected by JsonRestStore.

By default, an EasyRestService instance does not
alter the arguments and results in any way. Instead of altering DefaultHooks to perform required arguments and
results transformation, EasyRestService
provides a mechanism for overriding these transformers on a per-instance
basis.

Customizing EasyRestService

EasyRestService provides a simple mechanism for
the provision of custom transformers. The example in Listing 3 alters the behavior of EasyRestService to log the GET invocation structure before
execution.

Similarly, all transformers in DefaultHooks can
be overridden on a per-instance basis.

Example

Create two instances of EasyRestService: one for
a compliant service and one for a noncompliant service. The example uses
them as the service providers for two instances of JsonRestStore, and
issues a basic fetch against the stores.

The data stores

In Listing 4 and Listing 5,
the services are mocked up using two read-only files containing JSON
structures.

Listing 4. Compliant REST service,
compliantService.json

[
{ id: 1, name: "Phil" },
{ id: 2, name: "John" }
]

Listing 5. Noncompliant REST service,
noncompliantService.json

{ items : [
{ id: 1, name: "Phil" },
{ id: 2, name: "John" }
] }

Store and service interaction code

JavaScript will instantiate and query the stores using the code in Listing 6.

Summary

In this article, you learned how to wire up your own REST services to JsonRestStore. The examples showed a simple method of transforming your service interface to provide the signatures that JsonRestStore requires. For information on the full data structure expected by JsonRestStore, you should refer to the comments in DefaultHooks, the JsonRestStore documentation at DojoCampus, and the API documentation.

For information on the full data structure expected by JsonRestStore,
see the Related topics section.