APIs

Show:
  1. /**
  2. Super Ajax class with support for queue
  3. @class AjaxRPC
  4. @constructor
  5. @return {Object} instantiated AjaxRPC
  6. @example
  7. ------------------------------------------
  8. FOR XML:
  9.  
  10. To send XML Data use the following method/data:
  11.  
  12. SEND:
  13. this.ajax.getData(
  14. {userName: self.userName,userPass: self.userPass},
  15. 'http://my.server.com', 'onReply', 'xml', this
  16. );
  17.  
  18. RECEIVE:
  19. var xmlDoc = data.response;
  20. var resultTag = $(xmlDoc).find( "result" );
  21. var status = $(resultTag).attr("status");
  22. ------------------------------------------
  23. **/
  24. define(['jquery', 'backbone'], function ($, Backbone) {
  25.  
  26. BB.EVENTS.AJAX_ERROR = 'AJAX_ERROR';
  27.  
  28. var AjaxRPC = Backbone.Controller.extend({
  29.  
  30. /**
  31. Constructor
  32. @method initialize
  33. **/
  34. initialize: function () {
  35. this.m_timeout = 15000;
  36. this.m_abort = false;
  37. this.m_ajax = '';
  38. this.m_queue = $({});
  39. },
  40.  
  41. abortAll: function(){
  42. this.m_abort = true;
  43. // this.m_ajax.abort();
  44. },
  45.  
  46. resumeAll: function(){
  47. this.m_abort = false;
  48. },
  49.  
  50. getData : function(i_data, i_url, i_callback, i_type, i_context){
  51. var self = this;
  52. if (self.m_abort)
  53. return;
  54.  
  55. self.m_ajax = self.ajaxQueue({
  56. url: i_url,
  57. data: i_data,
  58. context: i_context,
  59. cache: false,
  60. timeout: self.m_timeout
  61. }, i_callback, i_type)
  62. },
  63.  
  64. ajaxQueue: function( i_ajaxOpts, i_callBack, i_type) {
  65.  
  66. var self = this;
  67. self.ajaxOpts = i_ajaxOpts;
  68. var jqXHR,
  69. dfd = $.Deferred(),
  70. promise = dfd.promise();
  71.  
  72. function doRequest( next ) {
  73. jqXHR = $.ajax( i_ajaxOpts );
  74. jqXHR.done( dfd.resolve ).fail( dfd.reject ).then( next, next);
  75. }
  76.  
  77. dfd.always(function(i_data){
  78. if (self.m_abort)
  79. return;
  80.  
  81. if (!self.checkReplyStatus(jqXHR.status))
  82. return;
  83.  
  84. switch (i_type){
  85. case 'xml': {
  86. i_callBack({
  87. textStatus: jqXHR.statusText,
  88. status: jqXHR.status,
  89. context: this,
  90. response: $.parseXML(i_data)
  91. // response: StringtoXML(i_data)
  92.  
  93. });
  94. break;
  95. }
  96. case 'json': {
  97.  
  98. var jData;
  99.  
  100. try {
  101. jData = eval("(" + i_data + ")");
  102. } catch (e) {
  103. BB.comBroker.fire(BB.EVENTS.AJAX_ERROR);
  104. return;
  105. }
  106.  
  107. i_callBack({
  108. textStatus: jqXHR.statusText,
  109. status: jqXHR.status,
  110. context: this,
  111. responce: jData
  112. });
  113. break;
  114. }
  115.  
  116. default: {
  117. i_callBack({
  118. textStatus: jqXHR.statusText,
  119. status: jqXHR.status,
  120. context: this,
  121. response: i_data
  122. });
  123. }
  124. }
  125. });
  126.  
  127. //dfd.done(...
  128. //dfd.fail(...
  129.  
  130. // queue our ajax request
  131. // log('adding to queue ' + self.getQueueSize() );
  132.  
  133. self.m_queue.queue( doRequest );
  134.  
  135. // add the abort method
  136. promise.abort = function( statusText ) {
  137. var self = this.self;
  138. // console.log('aborted');
  139. // proxy abort to the jqXHR if it is active
  140. if ( jqXHR ) {
  141. return jqXHR.abort( statusText );
  142. }
  143.  
  144. // if there wasn't already a jqXHR we need to remove from queue
  145. var queue = self.m_queue.queue(),
  146. index = $.inArray( doRequest, queue );
  147.  
  148. if ( index > -1 ) {
  149. queue.splice( index, 1 );
  150. }
  151.  
  152. // and then reject the deferred
  153. dfd.rejectWith( self.ajaxOpts.context || self.ajaxOpts, [ promise, statusText, "" ] );
  154. return promise;
  155. };
  156.  
  157. return promise;
  158. },
  159.  
  160. getQueueSize: function(){
  161. var self = this;
  162. var a = self.m_queue[0];
  163. for (var b in a){
  164. var c = a[b];
  165. return c['fxqueue'].length;
  166. }
  167. return 0;
  168. },
  169.  
  170. checkReplyStatus: function(status){
  171.  
  172. var msg = '';
  173. switch (String(status)){
  174. case '200': {return true; break}
  175. case '0': {msg="server reply timed out, please try again soon"; break}
  176. case '408': {msg="server reply timed out, please try again soon"; break}
  177. case '400': {msg="bad request to server, please try again soon"; break}
  178. case '404': {msg="file missing on server, please try again soon"; break}
  179. default: {msg="problem with server, please try again soon"; break;}
  180.  
  181. }
  182. BB.comBroker.fire(BB.EVENTS.AJAX_ERROR, this);
  183. return false;
  184. }
  185. });
  186.  
  187. return AjaxRPC;
  188.  
  189. });