http_client.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. */
  29. defaultErrorHandler: function (jqXHR, textStatus, errorThrown) {
  30. var json = $.parseJSON(jqXHR.responseText);
  31. if (json) {
  32. Em.assert("HttpClient:", json);
  33. } else {
  34. Em.assert("HttpClient:", errorThrown);
  35. }
  36. },
  37. emptyFunc: function () {
  38. },
  39. /**
  40. * @param {string} url
  41. * @param {Object} ajaxOptions
  42. * @param {App.ServerDataMapper} mapper - json processor
  43. * @param {function} errorHandler
  44. */
  45. request: function (url, ajaxOptions, mapper, errorHandler) {
  46. if (!errorHandler) {
  47. errorHandler = this.defaultErrorHandler;
  48. }
  49. var xhr = new XMLHttpRequest();
  50. var curTime = new Date().getTime();
  51. xhr.open('GET', url + (url.indexOf('?') >= 0 ? '&_=' : '?_=') + curTime, true);
  52. xhr.send(null);
  53. this.onReady(xhr, "", ajaxOptions, mapper, errorHandler);
  54. },
  55. /*
  56. This function checks if we get response from server
  57. Not using onreadystatechange cuz of possible closure
  58. */
  59. onReady: function (xhr, tm, tmp_val, mapper, errorHandler) {
  60. var self = this;
  61. clearTimeout(tm);
  62. var timeout = setTimeout(function () {
  63. if (xhr.readyState == 4) {
  64. if (xhr.status == 200) {
  65. try {
  66. App.store.commit();
  67. } catch (err) {}
  68. mapper.map($.parseJSON(xhr.responseText));
  69. tmp_val.complete.call(self);
  70. xhr.abort();
  71. } else {
  72. errorHandler(xhr , "error", xhr.statusText);
  73. }
  74. tmp_val = null;
  75. xhr = self.emptyFunc();
  76. clearTimeout(timeout);
  77. timeout = null;
  78. } else {
  79. self.onReady(xhr, timeout, tmp_val, mapper, errorHandler);
  80. }
  81. }, 10);
  82. },
  83. /**
  84. * @param {string} url
  85. * @param {App.ServerDataMapper} mapper - json processor
  86. * @param {Object} data - ajax data property
  87. * @param {function} errorHandler
  88. * @param {number} interval - frequency request
  89. */
  90. get: function (url, mapper, data, errorHandler, interval) {
  91. var eHandler = data.complete
  92. if (!errorHandler && data.error) {
  93. errorHandler = data.error;
  94. }
  95. var client = this;
  96. var request = function () {
  97. client.request(url, data, mapper, errorHandler);
  98. url=null;
  99. data=null;
  100. mapper=null;
  101. errorHandler=null;
  102. }
  103. interval = "" + interval;
  104. if (interval.match(/\d+/)) {
  105. $.periodic({period: interval}, request);
  106. } else {
  107. request();
  108. }
  109. },
  110. /**
  111. * @param {string} url
  112. * @param {Object} data - ajax data property
  113. * @param {App.ServerDataMapper} mapper - json processor
  114. * @param {function} errorHandler
  115. * @param {number} interval - frequecy request
  116. */
  117. post: function (url, data, mapper, errorHandler, interval) {
  118. this.get(url, data, mapper, errorHandler, interval);
  119. }
  120. });