http_client.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. /**
  20. * App.HttpClient perform an ajax request
  21. */
  22. App.HttpClient = Em.Object.create({
  23. /**
  24. *
  25. * @param jqXHR
  26. * @param textStatus
  27. * @param errorThrown
  28. * @param url api that invoked this callback function
  29. */
  30. defaultErrorHandler: function (jqXHR, textStatus, errorThrown, url) {
  31. try {
  32. var json = $.parseJSON(jqXHR.responseText);
  33. } catch (err) {
  34. }
  35. App.ajax.defaultErrorHandler(jqXHR, url);
  36. if (json) {
  37. Em.assert("HttpClient:", json);
  38. } else {
  39. if (!$.mocho) { // don't use this assert on tests
  40. Em.assert("HttpClient:", errorThrown);
  41. }
  42. }
  43. },
  44. /**
  45. * @param {string} url
  46. * @param {Object} ajaxOptions
  47. * @param {App.ServerDataMapper} mapper - json processor
  48. * @param {callback} errorHandler
  49. */
  50. request: function (url, ajaxOptions, mapper, errorHandler) {
  51. if (!errorHandler) {
  52. errorHandler = this.defaultErrorHandler;
  53. }
  54. var xhr = new XMLHttpRequest();
  55. var curTime = App.dateTime();
  56. xhr.open('GET', url + (url.indexOf('?') >= 0 ? '&_=' : '?_=') + curTime, true);
  57. xhr.send(null);
  58. this.onReady(xhr, "", ajaxOptions, mapper, errorHandler, url);
  59. },
  60. /*
  61. This function checks if we get response from server
  62. Not using onreadystatechange cuz of possible closure
  63. */
  64. onReady: function (xhr, tm, tmp_val, mapper, errorHandler, url) {
  65. var self = this;
  66. clearTimeout(tm);
  67. var timeout = setTimeout(function () {
  68. if (xhr.readyState == 4) {
  69. if (xhr.status == 200) {
  70. try {
  71. App.store.commit();
  72. } catch (err) {
  73. console.warn('App.store.commit error:', err);
  74. }
  75. mapper.map($.parseJSON(xhr.responseText));
  76. tmp_val.complete.call(self);
  77. xhr.abort();
  78. } else {
  79. errorHandler(xhr, "error", xhr.statusText, url);
  80. }
  81. tmp_val = null;
  82. xhr = null;
  83. clearTimeout(timeout);
  84. timeout = null;
  85. } else {
  86. self.onReady(xhr, timeout, tmp_val, mapper, errorHandler, url);
  87. }
  88. }, 10);
  89. },
  90. /**
  91. * @param {string} url
  92. * @param {App.ServerDataMapper} mapper - json processor
  93. * @param {Object} data - ajax data property
  94. * @param {callback} errorHandler
  95. * @param {number} interval - frequency request
  96. */
  97. get: function (url, mapper, data, errorHandler, interval) {
  98. if (!errorHandler && data.error) {
  99. errorHandler = data.error;
  100. }
  101. var client = this;
  102. var request = function () {
  103. client.request(url, data, mapper, errorHandler);
  104. url = null;
  105. data = null;
  106. mapper = null;
  107. errorHandler = null;
  108. };
  109. interval = "" + interval;
  110. if (interval.match(/\d+/)) {
  111. $.periodic({period: interval}, request);
  112. } else {
  113. request();
  114. }
  115. },
  116. /**
  117. * @param {string} url
  118. * @param {Object} data - ajax data property
  119. * @param {App.ServerDataMapper} mapper - json processor
  120. * @param {callback} errorHandler
  121. * @param {number} interval - frequecy request
  122. */
  123. post: function (url, data, mapper, errorHandler, interval) {
  124. this.get(url, data, mapper, errorHandler, interval);
  125. }
  126. });