ajax_queue.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. */
  106. addRequest: function(request) {
  107. Em.assert('Each ajax-request should has non-blank `name`', !Em.isBlank(Em.get(request, 'name')));
  108. Em.assert('Each ajax-request should has object `sender`', Em.typeOf(Em.get(request, 'sender')) !== 'object');
  109. this.get('queue').pushObject(request);
  110. return this;
  111. },
  112. /**
  113. * Add requests to the <code>queue</code>
  114. * @param {Object[]} requests list of objects that uses in <code>App.ajax.send</code>
  115. * @return {App.ajaxQueue}
  116. */
  117. addRequests: function(requests) {
  118. requests.map(function(request) {
  119. this.addRequest(request);
  120. }, this);
  121. return this;
  122. },
  123. /**
  124. * Enter point to start requests executing
  125. */
  126. start: function() {
  127. this.runNextRequest();
  128. },
  129. /**
  130. * Execute first request from the <code>queue</code>
  131. */
  132. runNextRequest: function() {
  133. var self = this;
  134. var queue = this.get('queue');
  135. if (queue.length === 0) {
  136. this.finishedCallback();
  137. return;
  138. }
  139. var r = App.ajax.send(queue.shift());
  140. this.propertyDidChange('queue');
  141. if (r) {
  142. r.complete(function(xhr) {
  143. if(xhr.status>=200 && xhr.status <= 299) {
  144. self.runNextRequest();
  145. }
  146. else {
  147. if (self.get('abortOnError')) {
  148. self.clear();
  149. }
  150. else {
  151. self.runNextRequest();
  152. }
  153. }
  154. });
  155. }
  156. else {
  157. if (this.get('abortOnError')) {
  158. this.clear();
  159. }
  160. else {
  161. this.runNextRequest();
  162. }
  163. }
  164. },
  165. /**
  166. * Remove all requests from <code>queue</code>
  167. */
  168. clear: function() {
  169. this.get('queue').clear();
  170. }
  171. });