ajax_queue.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * Simple util for executing ajax-requests queue
  21. *
  22. * @uses App.ajax
  23. * @type {Em.Object}
  24. *
  25. * Don't add requests directly to <code>queue</code>!
  26. * Bad example:
  27. * <code>
  28. * var q = App.ajax.Queue.create();
  29. * q.get('queue').pushObject({...}); // Don't do this!!!!
  30. * </code>
  31. *
  32. * Don't call <code>runNextRequest</code> directly!
  33. * Bad example:
  34. * <code>
  35. * var q = App.ajax.Queue.create();
  36. * q.runNextRequest(); // Don't do this!!!!
  37. * </code>
  38. *
  39. * Usage example 1:
  40. * <code>
  41. * var q = App.ajax.Queue.create();
  42. * q.addRequest({
  43. * name: 'some_request',
  44. * sender: this,
  45. * success: 'someFunc'
  46. * }).addRequest({
  47. * name: 'some_request2',
  48. * sender: this,
  49. * success: 'someFunc2'
  50. * }).start();
  51. * </code>
  52. *
  53. * Usage example 2:
  54. * <code>
  55. * var q = App.ajax.Queue.create();
  56. * q.addRequest({
  57. * name: 'some_request',
  58. * sender: this,
  59. * success: 'someFunc'
  60. * });
  61. * q.addRequest({
  62. * name: 'some_request2',
  63. * sender: this,
  64. * success: 'someFunc2'
  65. * });
  66. * q.start();
  67. * </code>
  68. *
  69. * Usage example 3:
  70. * <code>
  71. * var q = App.ajax.Queue.create();
  72. * q.addRequests(Em.A([{
  73. * name: 'some_request',
  74. * sender: this,
  75. * success: 'someFunc'
  76. * },
  77. * {
  78. * name: 'some_request2',
  79. * sender: this,
  80. * success: 'someFunc2'
  81. * }]));
  82. * q.start();
  83. * </code>
  84. */
  85. App.ajaxQueue = Em.Object.extend({
  86. /**
  87. * List of requests
  88. * @type {Object[]}
  89. */
  90. queue: Em.A([]),
  91. /**
  92. * About query executing if some request failed
  93. * @type {bool}
  94. */
  95. abortOnError: true,
  96. /**
  97. * Function called after queue is complete
  98. * @type {callback}
  99. */
  100. finishedCallback: Em.K,
  101. /**
  102. * Add request to the <code>queue</code>
  103. * @param {Object} request object that uses in <code>App.ajax.send</code>
  104. * @return {App.ajaxQueue}
  105. * @method addRequest
  106. */
  107. addRequest: function(request) {
  108. Em.assert('Each ajax-request should has non-blank `name`', !Em.isBlank(Em.get(request, 'name')));
  109. Em.assert('Each ajax-request should has object `sender`', Em.typeOf(Em.get(request, 'sender')) !== 'object');
  110. this.get('queue').pushObject(request);
  111. return this;
  112. },
  113. /**
  114. * Add requests to the <code>queue</code>
  115. * @param {Object[]} requests list of objects that uses in <code>App.ajax.send</code>
  116. * @return {App.ajaxQueue}
  117. * @method addRequests
  118. */
  119. addRequests: function(requests) {
  120. requests.map(function(request) {
  121. this.addRequest(request);
  122. }, this);
  123. return this;
  124. },
  125. /**
  126. * Enter point to start requests executing
  127. * @method start
  128. */
  129. start: function() {
  130. this.runNextRequest();
  131. },
  132. /**
  133. * Execute first request from the <code>queue</code>
  134. * @method runNextRequest
  135. */
  136. runNextRequest: function() {
  137. var self = this;
  138. var queue = this.get('queue');
  139. if (queue.length === 0) {
  140. this.finishedCallback();
  141. return;
  142. }
  143. var r = App.ajax.send(queue.shift());
  144. this.propertyDidChange('queue');
  145. if (r) {
  146. r.complete(function(xhr) {
  147. if(xhr.status>=200 && xhr.status <= 299) {
  148. self.runNextRequest();
  149. }
  150. else {
  151. if (self.get('abortOnError')) {
  152. self.clear();
  153. }
  154. else {
  155. self.runNextRequest();
  156. }
  157. }
  158. });
  159. }
  160. else {
  161. if (this.get('abortOnError')) {
  162. this.clear();
  163. }
  164. else {
  165. this.runNextRequest();
  166. }
  167. }
  168. },
  169. /**
  170. * Remove all requests from <code>queue</code>
  171. * @method clear
  172. */
  173. clear: function() {
  174. this.get('queue').clear();
  175. }
  176. });