/Recipes/Recipes/WebFolder/scripts/ext/src/direct/PollingProvider.js

https://github.com/gilles-danjou/sencha-wakanda · JavaScript · 172 lines · 70 code · 17 blank · 85 comment · 11 complexity · 21605a299c81c9ca6b4d5f28f9acaa8e MD5 · raw file

  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * @class Ext.direct.PollingProvider
  11. * @extends Ext.direct.JsonProvider
  12. *
  13. * <p>Provides for repetitive polling of the server at distinct {@link #interval intervals}.
  14. * The initial request for data originates from the client, and then is responded to by the
  15. * server.</p>
  16. *
  17. * <p>All configurations for the PollingProvider should be generated by the server-side
  18. * API portion of the Ext.Direct stack.</p>
  19. *
  20. * <p>An instance of PollingProvider may be created directly via the new keyword or by simply
  21. * specifying <tt>type = 'polling'</tt>. For example:</p>
  22. * <pre><code>
  23. var pollA = new Ext.direct.PollingProvider({
  24. type:'polling',
  25. url: 'php/pollA.php',
  26. });
  27. Ext.direct.Manager.addProvider(pollA);
  28. pollA.disconnect();
  29. Ext.direct.Manager.addProvider(
  30. {
  31. type:'polling',
  32. url: 'php/pollB.php',
  33. id: 'pollB-provider'
  34. }
  35. );
  36. var pollB = Ext.direct.Manager.getProvider('pollB-provider');
  37. * </code></pre>
  38. */
  39. Ext.define('Ext.direct.PollingProvider', {
  40. /* Begin Definitions */
  41. extend: 'Ext.direct.JsonProvider',
  42. alias: 'direct.pollingprovider',
  43. uses: ['Ext.direct.ExceptionEvent'],
  44. requires: ['Ext.Ajax', 'Ext.util.DelayedTask'],
  45. /* End Definitions */
  46. /**
  47. * @cfg {Number} interval
  48. * How often to poll the server-side in milliseconds (defaults to <tt>3000</tt> - every
  49. * 3 seconds).
  50. */
  51. interval: 3000,
  52. /**
  53. * @cfg {Object} baseParams An object containing properties which are to be sent as parameters
  54. * on every polling request
  55. */
  56. /**
  57. * @cfg {String/Function} url
  58. * The url which the PollingProvider should contact with each request. This can also be
  59. * an imported Ext.Direct method which will accept the baseParams as its only argument.
  60. */
  61. // private
  62. constructor : function(config){
  63. this.callParent(arguments);
  64. this.addEvents(
  65. /**
  66. * @event beforepoll
  67. * Fired immediately before a poll takes place, an event handler can return false
  68. * in order to cancel the poll.
  69. * @param {Ext.direct.PollingProvider}
  70. */
  71. 'beforepoll',
  72. /**
  73. * @event poll
  74. * This event has not yet been implemented.
  75. * @param {Ext.direct.PollingProvider}
  76. */
  77. 'poll'
  78. );
  79. },
  80. // inherited
  81. isConnected: function(){
  82. return !!this.pollTask;
  83. },
  84. /**
  85. * Connect to the server-side and begin the polling process. To handle each
  86. * response subscribe to the data event.
  87. */
  88. connect: function(){
  89. var me = this, url = me.url;
  90. if (url && !me.pollTask) {
  91. me.pollTask = Ext.TaskManager.start({
  92. run: function(){
  93. if (me.fireEvent('beforepoll', me) !== false) {
  94. if (Ext.isFunction(url)) {
  95. url(me.baseParams);
  96. } else {
  97. Ext.Ajax.request({
  98. url: url,
  99. callback: me.onData,
  100. scope: me,
  101. params: me.baseParams
  102. });
  103. }
  104. }
  105. },
  106. interval: me.interval,
  107. scope: me
  108. });
  109. me.fireEvent('connect', me);
  110. } else if (!url) {
  111. //<debug>
  112. Ext.Error.raise('Error initializing PollingProvider, no url configured.');
  113. //</debug>
  114. }
  115. },
  116. /**
  117. * Disconnect from the server-side and stop the polling process. The disconnect
  118. * event will be fired on a successful disconnect.
  119. */
  120. disconnect: function(){
  121. var me = this;
  122. if (me.pollTask) {
  123. Ext.TaskManager.stop(me.pollTask);
  124. delete me.pollTask;
  125. me.fireEvent('disconnect', me);
  126. }
  127. },
  128. // private
  129. onData: function(opt, success, response){
  130. var me = this,
  131. i = 0,
  132. len,
  133. events;
  134. if (success) {
  135. events = me.createEvents(response);
  136. for (len = events.length; i < len; ++i) {
  137. me.fireEvent('data', me, events[i]);
  138. }
  139. } else {
  140. me.fireEvent('data', me, Ext.create('Ext.direct.ExceptionEvent', {
  141. data: null,
  142. code: Ext.direct.Manager.self.exceptions.TRANSPORT,
  143. message: 'Unable to connect to the server.',
  144. xhr: response
  145. }));
  146. }
  147. }
  148. });