JsonRest.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Copyright (c) 2004-2012, 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
  5. */
  6. if(!dojo._hasResource["dojo.store.JsonRest"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo.store.JsonRest"] = true;
  8. dojo.provide("dojo.store.JsonRest");
  9. dojo.require("dojo.store.util.QueryResults");
  10. dojo.declare("dojo.store.JsonRest", null, {
  11. constructor: function(/*dojo.store.JsonRest*/ options){
  12. // summary:
  13. // This is a basic store for RESTful communicating with a server through JSON
  14. // formatted data.
  15. // options:
  16. // This provides any configuration information that will be mixed into the store
  17. dojo.mixin(this, options);
  18. },
  19. // target: String
  20. // The target base URL to use for all requests to the server. This string will be
  21. // prepended to the id to generate the URL (relative or absolute) for requests
  22. // sent to the server
  23. target: "",
  24. // idProperty: String
  25. // Indicates the property to use as the identity property. The values of this
  26. // property should be unique.
  27. idProperty: "id",
  28. get: function(id, options){
  29. // summary:
  30. // Retrieves an object by its identity. This will trigger a GET request to the server using
  31. // the url `this.target + id`.
  32. // id: Number
  33. // The identity to use to lookup the object
  34. // returns: Object
  35. // The object in the store that matches the given id.
  36. var headers = options || {};
  37. headers.Accept = "application/javascript, application/json";
  38. return dojo.xhrGet({
  39. url:this.target + id,
  40. handleAs: "json",
  41. headers: headers
  42. });
  43. },
  44. getIdentity: function(object){
  45. // summary:
  46. // Returns an object's identity
  47. // object: Object
  48. // The object to get the identity from
  49. // returns: Number
  50. return object[this.idProperty];
  51. },
  52. put: function(object, options){
  53. // summary:
  54. // Stores an object. This will trigger a PUT request to the server
  55. // if the object has an id, otherwise it will trigger a POST request.
  56. // object: Object
  57. // The object to store.
  58. // options: dojo.store.api.Store.PutDirectives?
  59. // Additional metadata for storing the data. Includes an "id"
  60. // property if a specific id is to be used.
  61. // returns: Number
  62. options = options || {};
  63. var id = ("id" in options) ? options.id : this.getIdentity(object);
  64. var hasId = typeof id != "undefined";
  65. return dojo.xhr(hasId && !options.incremental ? "PUT" : "POST", {
  66. url: hasId ? this.target + id : this.target,
  67. postData: dojo.toJson(object),
  68. handleAs: "json",
  69. headers:{
  70. "Content-Type": "application/json",
  71. "If-Match": options.overwrite === true ? "*" : null,
  72. "If-None-Match": options.overwrite === false ? "*" : null
  73. }
  74. });
  75. },
  76. add: function(object, options){
  77. // summary:
  78. // Adds an object. This will trigger a PUT request to the server
  79. // if the object has an id, otherwise it will trigger a POST request.
  80. // object: Object
  81. // The object to store.
  82. // options: dojo.store.api.Store.PutDirectives?
  83. // Additional metadata for storing the data. Includes an "id"
  84. // property if a specific id is to be used.
  85. options = options || {};
  86. options.overwrite = false;
  87. return this.put(object, options);
  88. },
  89. remove: function(id){
  90. // summary:
  91. // Deletes an object by its identity. This will trigger a DELETE request to the server.
  92. // id: Number
  93. // The identity to use to delete the object
  94. return dojo.xhrDelete({
  95. url:this.target + id
  96. });
  97. },
  98. query: function(query, options){
  99. // summary:
  100. // Queries the store for objects. This will trigger a GET request to the server, with the
  101. // query added as a query string.
  102. // query: Object
  103. // The query to use for retrieving objects from the store.
  104. // options: dojo.store.api.Store.QueryOptions?
  105. // The optional arguments to apply to the resultset.
  106. // returns: dojo.store.api.Store.QueryResults
  107. // The results of the query, extended with iterative methods.
  108. var headers = {Accept: "application/javascript, application/json"};
  109. options = options || {};
  110. if(options.start >= 0 || options.count >= 0){
  111. headers.Range = "items=" + (options.start || '0') + '-' +
  112. (("count" in options && options.count != Infinity) ?
  113. (options.count + (options.start || 0) - 1) : '');
  114. }
  115. if(dojo.isObject(query)){
  116. query = dojo.objectToQuery(query);
  117. query = query ? "?" + query: "";
  118. }
  119. if(options && options.sort){
  120. query += (query ? "&" : "?") + "sort(";
  121. for(var i = 0; i<options.sort.length; i++){
  122. var sort = options.sort[i];
  123. query += (i > 0 ? "," : "") + (sort.descending ? '-' : '+') + encodeURIComponent(sort.attribute);
  124. }
  125. query += ")";
  126. }
  127. var results = dojo.xhrGet({
  128. url: this.target + (query || ""),
  129. handleAs: "json",
  130. headers: headers
  131. });
  132. results.total = results.then(function(){
  133. var range = results.ioArgs.xhr.getResponseHeader("Content-Range");
  134. return range && (range=range.match(/\/(.*)/)) && +range[1];
  135. });
  136. return dojo.store.util.QueryResults(results);
  137. }
  138. });
  139. }