]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/store/JsonRest.js.uncompressed.js
1 define("dojo/store/JsonRest", ["../_base/xhr", "../json", "../_base/declare", "./util/QueryResults"
2 ], function(xhr
, JSON
, declare
, QueryResults
) {
6 // The module defines a JSON/REST based object store
8 return declare("dojo.store.JsonRest", null, {
10 // This is a basic store for RESTful communicating with a server through JSON
11 // formatted data. It implements dojo.store.api.Store.
13 constructor: function(/*dojo.store.JsonRest*/ options
){
15 // This is a basic store for RESTful communicating with a server through JSON
18 // This provides any configuration information that will be mixed into the store
19 declare
.safeMixin(this, options
);
22 // The target base URL to use for all requests to the server. This string will be
23 // prepended to the id to generate the URL (relative or absolute) for requests
27 // Indicates the property to use as the identity property. The values of this
28 // property should be unique.
31 // The query parameter to used for holding sort information. If this is omitted, than
32 // the sort information is included in a functional query token to avoid colliding
33 // with the set of name/value pairs.
35 get: function(id
, options
){
37 // Retrieves an object by its identity. This will trigger a GET request to the server using
38 // the url `this.target + id`.
40 // The identity to use to lookup the object
42 // The object in the store that matches the given id.
43 var headers
= options
|| {};
44 headers
.Accept
= this.accepts
;
52 // Defines the Accept header to use on HTTP requests
53 accepts
: "application/javascript, application/json",
54 getIdentity: function(object
){
56 // Returns an object's identity
58 // The object to get the identity from
60 return object
[this.idProperty
];
62 put: function(object
, options
){
64 // Stores an object. This will trigger a PUT request to the server
65 // if the object has an id, otherwise it will trigger a POST request.
67 // The object to store.
68 // options: dojo.store.api.Store.PutDirectives?
69 // Additional metadata for storing the data. Includes an "id"
70 // property if a specific id is to be used.
72 options
= options
|| {};
73 var id
= ("id" in options
) ? options
.id
: this.getIdentity(object
);
74 var hasId
= typeof id
!= "undefined";
75 return xhr(hasId
&& !options
.incremental
? "PUT" : "POST", {
76 url
: hasId
? this.target
+ id
: this.target
,
77 postData
: JSON
.stringify(object
),
80 "Content-Type": "application/json",
82 "If-Match": options
.overwrite
=== true ? "*" : null,
83 "If-None-Match": options
.overwrite
=== false ? "*" : null
87 add: function(object
, options
){
89 // Adds an object. This will trigger a PUT request to the server
90 // if the object has an id, otherwise it will trigger a POST request.
92 // The object to store.
93 // options: dojo.store.api.Store.PutDirectives?
94 // Additional metadata for storing the data. Includes an "id"
95 // property if a specific id is to be used.
96 options
= options
|| {};
97 options
.overwrite
= false;
98 return this.put(object
, options
);
100 remove: function(id
){
102 // Deletes an object by its identity. This will trigger a DELETE request to the server.
104 // The identity to use to delete the object
105 return xhr("DELETE",{
109 query: function(query
, options
){
111 // Queries the store for objects. This will trigger a GET request to the server, with the
112 // query added as a query string.
114 // The query to use for retrieving objects from the store.
115 // options: dojo.store.api.Store.QueryOptions?
116 // The optional arguments to apply to the resultset.
117 // returns: dojo.store.api.Store.QueryResults
118 // The results of the query, extended with iterative methods.
119 var headers
= {Accept
: this.accepts
};
120 options
= options
|| {};
122 if(options
.start
>= 0 || options
.count
>= 0){
123 headers
.Range
= "items=" + (options
.start
|| '0') + '-' +
124 (("count" in options
&& options
.count
!= Infinity
) ?
125 (options
.count
+ (options
.start
|| 0) - 1) : '');
127 if(query
&& typeof query
== "object"){
128 query
= xhr
.objectToQuery(query
);
129 query
= query
? "?" + query
: "";
131 if(options
&& options
.sort
){
132 var sortParam
= this.sortParam
;
133 query
+= (query
? "&" : "?") + (sortParam
? sortParam
+ '=' : "sort(");
134 for(var i
= 0; i
<options
.sort
.length
; i
++){
135 var sort
= options
.sort
[i
];
136 query
+= (i
> 0 ? "," : "") + (sort
.descending
? '-' : '+') + encodeURIComponent(sort
.attribute
);
142 var results
= xhr("GET", {
143 url
: this.target
+ (query
|| ""),
147 results
.total
= results
.then(function(){
148 var range
= results
.ioArgs
.xhr
.getResponseHeader("Content-Range");
149 return range
&& (range
=range
.match(/\/(.*)/)) && +range
[1];
151 return QueryResults(results
);