http.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * Durandal 2.1.0 Copyright (c) 2012 Blue Spire Consulting, Inc. All Rights Reserved.
  3. * Available via the MIT license.
  4. * see: http://durandaljs.com or https://github.com/BlueSpire/Durandal for details.
  5. */
  6. /**
  7. * Enables common http request scenarios.
  8. * @module http
  9. * @requires jquery
  10. * @requires knockout
  11. */
  12. define(['jquery', 'knockout'], function ($, ko) {
  13. /**
  14. * @class HTTPModule
  15. * @static
  16. */
  17. return {
  18. /**
  19. * The name of the callback parameter to inject into jsonp requests by default.
  20. * @property {string} callbackParam
  21. * @default callback
  22. */
  23. callbackParam: 'callback',
  24. /**
  25. * Converts the data to JSON.
  26. * @method toJSON
  27. * @param {object} data The data to convert to JSON.
  28. * @return {string} JSON.
  29. */
  30. toJSON: function(data) {
  31. return ko.toJSON(data);
  32. },
  33. /**
  34. * Makes an HTTP GET request.
  35. * @method get
  36. * @param {string} url The url to send the get request to.
  37. * @param {object} [query] An optional key/value object to transform into query string parameters.
  38. * @param {object} [headers] The data to add to the request header. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
  39. * @return {Promise} A promise of the get response data.
  40. */
  41. get: function (url, query, headers) {
  42. return $.ajax(url, { data: query, headers: ko.toJS(headers) });
  43. },
  44. /**
  45. * Makes an JSONP request.
  46. * @method jsonp
  47. * @param {string} url The url to send the get request to.
  48. * @param {object} [query] An optional key/value object to transform into query string parameters.
  49. * @param {string} [callbackParam] The name of the callback parameter the api expects (overrides the default callbackParam).
  50. * @param {object} [headers] The data to add to the request header. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
  51. * @return {Promise} A promise of the response data.
  52. */
  53. jsonp: function (url, query, callbackParam, headers) {
  54. if (url.indexOf('=?') == -1) {
  55. callbackParam = callbackParam || this.callbackParam;
  56. if (url.indexOf('?') == -1) {
  57. url += '?';
  58. } else {
  59. url += '&';
  60. }
  61. url += callbackParam + '=?';
  62. }
  63. return $.ajax({
  64. url: url,
  65. dataType: 'jsonp',
  66. data: query,
  67. headers: ko.toJS(headers)
  68. });
  69. },
  70. /**
  71. * Makes an HTTP PUT request.
  72. * @method put
  73. * @param {string} url The url to send the put request to.
  74. * @param {object} data The data to put. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
  75. * @param {object} [headers] The data to add to the request header. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
  76. * @return {Promise} A promise of the response data.
  77. */
  78. put:function(url, data, headers) {
  79. return $.ajax({
  80. url: url,
  81. data: this.toJSON(data),
  82. type: 'PUT',
  83. contentType: 'application/json',
  84. dataType: 'json',
  85. headers: ko.toJS(headers)
  86. });
  87. },
  88. /**
  89. * Makes an HTTP POST request.
  90. * @method post
  91. * @param {string} url The url to send the post request to.
  92. * @param {object} data The data to post. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
  93. * @param {object} [headers] The data to add to the request header. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
  94. * @return {Promise} A promise of the response data.
  95. */
  96. post: function (url, data, headers) {
  97. return $.ajax({
  98. url: url,
  99. data: this.toJSON(data),
  100. type: 'POST',
  101. contentType: 'application/json',
  102. dataType: 'json',
  103. headers: ko.toJS(headers)
  104. });
  105. },
  106. /**
  107. * Makes an HTTP DELETE request.
  108. * @method remove
  109. * @param {string} url The url to send the delete request to.
  110. * @param {object} [query] An optional key/value object to transform into query string parameters.
  111. * @param {object} [headers] The data to add to the request header. It will be converted to JSON. If the data contains Knockout observables, they will be converted into normal properties before serialization.
  112. * @return {Promise} A promise of the get response data.
  113. */
  114. remove:function(url, query, headers) {
  115. return $.ajax({
  116. url: url,
  117. data: query,
  118. type: 'DELETE',
  119. headers: ko.toJS(headers)
  120. });
  121. }
  122. };
  123. });