dojox.data.JsonRestStore
The JsonRestStore will cause all saved modifications to be sent to the server using Rest commands (PUT, POST, or DELETE). When using a Rest store on a public network, it is important to implement proper security measures to control access to resources. On the server side implementing a REST interface means providing GET, PUT, POST, and DELETE handlers. GET - Retrieve an object or array/result set, this can be by id (like /table/1) or with a query (like /table/?name=foo). PUT - This should modify a object, the URL will correspond to the id (like /table/1), and the body will provide the modified object POST - This should create a new object. The URL will correspond to the target store (like /table/) and the body should be the properties of the new object. The server’s response should include a Location header that indicates the id of the newly created object. This id will be used for subsequent PUT and DELETE requests. JsonRestStore also includes a Content-Location header that indicates the temporary randomly generated id used by client, and this location is used for subsequent PUT/DELETEs if no Location header is provided by the server or if a modification is sent prior to receiving a response from the server. DELETE - This should delete an object by id. These articles include more detailed information on using the JsonRestStore: http://www.sitepen.com/blog/2008/06/13/restful-json-dojo-data/ http://blog.medryx.org/2008/07/24/jsonreststore-overview/
Usage
| parameter | type | description |
|---|---|---|
| options | Keyword | arguments The schema parameter This is a schema object for this store. This should be JSON Schema format. The service parameter This is the service object that is used to retrieve lazy data and save results The function should be directly callable with a single parameter of an object id to be loaded The function should also have the following methods: put(id,value) - puts the value at the given id post(id,value) - posts (appends) the value at the given id delete(id) - deletes the value corresponding to the given id Note that it is critical that the service parses responses as JSON. If you are using dojox.rpc.Service, the easiest way to make sure this happens is to make the responses have a content type of application/json. If you are creating your own service, make sure you use handleAs: "json" with your XHR requests. The target parameter This is the target URL for this Service store. This may be used in place of a service parameter to connect directly to RESTful URL without using a dojox.rpc.Service object. The idAttribute parameter Defaults to ‘id’. The name of the attribute that holds an objects id. This can be a preexisting id provided by the server. If an ID isn’t already provided when an object is fetched or added to the store, the autoIdentity system will generate an id for it and add it to the index. The syncMode parameter Setting this to true will set the store to using synchronous calls by default. Sync calls return their data immediately from the calling function, so callbacks are unnecessary |
Examples
Example 1
A JsonRestStore takes a REST service or a URL and uses it the remote communication for a read/write dojo.data implementation. A JsonRestStore can be created with a simple URL like:
new JsonRestStore({target:"/MyData/"});
Example 2
To use a JsonRestStore with a service, you should create a service with a REST transport. This can be configured with an SMD:
{ services: { jsonRestStore: { transport: "REST", envelope: "URL", target: "store.php", contentType:"application/json", parameters: [ {name: "location", type: "string", optional: true} ] } } }
The SMD can then be used to create service, and the service can be passed to a JsonRestStore. For example:
var myServices = new dojox.rpc.Service(dojo.moduleUrl("dojox.rpc.tests.resources", "test.smd")); var jsonStore = new dojox.data.JsonRestStore({service:myServices.jsonRestStore});
Example 3
The JsonRestStore also supports lazy loading. References can be made to objects that have not been loaded. For example if a service returned:
{"name":"Example","lazyLoadedObject":{"$ref":"obj2"}}
And this object has accessed using the dojo.data API:
var obj = jsonStore.getValue(myObject,"lazyLoadedObject");
The object would automatically be requested from the server (with an object id of “obj2”).