http_client.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /**
  38. * @param {string} url
  39. * @param {Object} ajaxOptions
  40. * @param {App.ServerDataMapper} mapper - json processor
  41. * @param {function} errorHandler
  42. * @param {number} interval - frequecy request
  43. */
  44. request: function (url, ajaxOptions, mapper, errorHandler) {
  45. if (!errorHandler) {
  46. errorHandler = this.defaultErrorHandler;
  47. }
  48. var options = {dataType: 'json'}; // default ajax options;
  49. $.extend(options, {
  50. url: url,
  51. success: function (data) {
  52. try {
  53. App.store.commit();
  54. } catch (err) {}
  55. mapper.map(data);
  56. },
  57. error: errorHandler
  58. });
  59. $.extend(options, ajaxOptions);
  60. $.ajax(options);
  61. },
  62. /**
  63. * @param {string} url
  64. * @param {App.ServerDataMapper} mapper - json processor
  65. * @param {Object} data - ajax data property
  66. * @param {function} errorHandler
  67. * @param {number} interval - frequency request
  68. */
  69. get: function (url, mapper, data, errorHandler, interval) {
  70. var eHandler = data.complete
  71. var client = this;
  72. var request = function () {
  73. client.request(url, data, mapper, errorHandler);
  74. }
  75. interval = "" + interval;
  76. if (interval.match(/\d+/)) {
  77. $.periodic({period: interval}, request);
  78. } else {
  79. request();
  80. }
  81. },
  82. /**
  83. * @param {string} url
  84. * @param {Object} data - ajax data property
  85. * @param {App.ServerDataMapper} mapper - json processor
  86. * @param {function} errorHandler
  87. * @param {number} interval - frequecy request
  88. */
  89. post: function (url, data, mapper, errorHandler, interval) {
  90. this.get(url, data, mapper, errorHandler, interval);
  91. }
  92. // not realized yet
  93. // put:function (url, mapper, errorHandler) {
  94. // this.request(url, {}, mapper, errorHandler);
  95. // },
  96. //
  97. // delete:function (url, mapper, errorHandler) {
  98. // this.request(url, {}, mapper, errorHandler);
  99. // }
  100. });
  101. /*App.HttpClient.get(
  102. 'http://nagiosserver/hdp/nagios/nagios_alerts.php?q1=alerts&alert_type=all',
  103. App.alertsMapper,
  104. { dataType: 'jsonp', jsonp: 'jsonp' }
  105. );*/