]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/store/JsonRest.js
2 Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
8 if(!dojo
._hasResource
["dojo.store.JsonRest"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo
._hasResource
["dojo.store.JsonRest"] = true;
10 dojo
.provide("dojo.store.JsonRest");
11 dojo
.require("dojo.store.util.QueryResults");
14 dojo
.declare("dojo.store.JsonRest", null, {
15 constructor: function(/*dojo.store.JsonRest*/ options
){
17 // This is a basic store for RESTful communicating with a server through JSON
20 // This provides any configuration information that will be mixed into the store
21 dojo
.mixin(this, options
);
24 // The target base URL to use for all requests to the server. This string will be
25 // prepended to the id to generate the URL (relative or absolute) for requests
29 // Indicates the property to use as the identity property. The values of this
30 // property should be unique.
33 get: function(id
, options
){
35 // Retrieves an object by its identity. This will trigger a GET request to the server using
36 // the url `this.target + id`.
38 // The identity to use to lookup the object
40 // The object in the store that matches the given id.
41 var headers
= options
|| {};
42 headers
.Accept
= "application/javascript, application/json";
49 getIdentity: function(object
){
51 // Returns an object's identity
53 // The object to get the identity from
55 return object
[this.idProperty
];
57 put: function(object
, options
){
59 // Stores an object. This will trigger a PUT request to the server
60 // if the object has an id, otherwise it will trigger a POST request.
62 // The object to store.
63 // options: dojo.store.api.Store.PutDirectives?
64 // Additional metadata for storing the data. Includes an "id"
65 // property if a specific id is to be used.
67 options
= options
|| {};
68 var id
= ("id" in options
) ? options
.id
: this.getIdentity(object
);
69 var hasId
= typeof id
!= "undefined";
70 return dojo
.xhr(hasId
&& !options
.incremental
? "PUT" : "POST", {
71 url
: hasId
? this.target
+ id
: this.target
,
72 postData
: dojo
.toJson(object
),
75 "Content-Type": "application/json",
76 "If-Match": options
.overwrite
=== true ? "*" : null,
77 "If-None-Match": options
.overwrite
=== false ? "*" : null
81 add: function(object
, options
){
83 // Adds an object. This will trigger a PUT request to the server
84 // if the object has an id, otherwise it will trigger a POST request.
86 // The object to store.
87 // options: dojo.store.api.Store.PutDirectives?
88 // Additional metadata for storing the data. Includes an "id"
89 // property if a specific id is to be used.
90 options
= options
|| {};
91 options
.overwrite
= false;
92 return this.put(object
, options
);
96 // Deletes an object by its identity. This will trigger a DELETE request to the server.
98 // The identity to use to delete the object
99 return dojo
.xhrDelete({
103 query: function(query
, options
){
105 // Queries the store for objects. This will trigger a GET request to the server, with the
106 // query added as a query string.
108 // The query to use for retrieving objects from the store.
109 // options: dojo.store.api.Store.QueryOptions?
110 // The optional arguments to apply to the resultset.
111 // returns: dojo.store.api.Store.QueryResults
112 // The results of the query, extended with iterative methods.
113 var headers
= {Accept
: "application/javascript, application/json"};
114 options
= options
|| {};
116 if(options
.start
>= 0 || options
.count
>= 0){
117 headers
.Range
= "items=" + (options
.start
|| '0') + '-' +
118 (("count" in options
&& options
.count
!= Infinity
) ?
119 (options
.count
+ (options
.start
|| 0) - 1) : '');
121 if(dojo
.isObject(query
)){
122 query
= dojo
.objectToQuery(query
);
123 query
= query
? "?" + query
: "";
125 if(options
&& options
.sort
){
126 query
+= (query
? "&" : "?") + "sort(";
127 for(var i
= 0; i
<options
.sort
.length
; i
++){
128 var sort
= options
.sort
[i
];
129 query
+= (i
> 0 ? "," : "") + (sort
.descending
? '-' : '+') + encodeURIComponent(sort
.attribute
);
133 var results
= dojo
.xhrGet({
134 url
: this.target
+ (query
|| ""),
138 results
.total
= results
.then(function(){
139 var range
= results
.ioArgs
.xhr
.getResponseHeader("Content-Range");
140 return range
&& (range
=range
.match(/\/(.*)/)) && +range
[1];
142 return dojo
.store
.util
.QueryResults(results
);