/src/main/webapp/ext-4.0/src/direct/PollingProvider.js

https://github.com/garyhellman/cprs · JavaScript · 171 lines · 70 code · 17 blank · 84 comment · 11 complexity · aff5e39f14bb954b92bbf66cb56b263f 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. Commercial Usage
  6. Licensees holding valid commercial licenses may use this file in accordance with the Commercial Software License Agreement provided with the Software or, alternatively, in accordance with the terms contained in a written agreement between you and Sencha.
  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 every 3 seconds.
  49. */
  50. interval: 3000,
  51. /**
  52. * @cfg {Object} baseParams
  53. * An object containing properties which are to be sent as parameters on every polling request
  54. */
  55. /**
  56. * @cfg {String/Function} url
  57. * The url which the PollingProvider should contact with each request. This can also be
  58. * an imported Ext.Direct method which will accept the baseParams as its only argument.
  59. */
  60. // private
  61. constructor : function(config){
  62. this.callParent(arguments);
  63. this.addEvents(
  64. /**
  65. * @event beforepoll
  66. * Fired immediately before a poll takes place, an event handler can return false
  67. * in order to cancel the poll.
  68. * @param {Ext.direct.PollingProvider} this
  69. */
  70. 'beforepoll',
  71. /**
  72. * @event poll
  73. * This event has not yet been implemented.
  74. * @param {Ext.direct.PollingProvider} this
  75. */
  76. 'poll'
  77. );
  78. },
  79. // inherited
  80. isConnected: function(){
  81. return !!this.pollTask;
  82. },
  83. /**
  84. * Connect to the server-side and begin the polling process. To handle each
  85. * response subscribe to the data event.
  86. */
  87. connect: function(){
  88. var me = this, url = me.url;
  89. if (url && !me.pollTask) {
  90. me.pollTask = Ext.TaskManager.start({
  91. run: function(){
  92. if (me.fireEvent('beforepoll', me) !== false) {
  93. if (Ext.isFunction(url)) {
  94. url(me.baseParams);
  95. } else {
  96. Ext.Ajax.request({
  97. url: url,
  98. callback: me.onData,
  99. scope: me,
  100. params: me.baseParams
  101. });
  102. }
  103. }
  104. },
  105. interval: me.interval,
  106. scope: me
  107. });
  108. me.fireEvent('connect', me);
  109. } else if (!url) {
  110. //<debug>
  111. Ext.Error.raise('Error initializing PollingProvider, no url configured.');
  112. //</debug>
  113. }
  114. },
  115. /**
  116. * Disconnect from the server-side and stop the polling process. The disconnect
  117. * event will be fired on a successful disconnect.
  118. */
  119. disconnect: function(){
  120. var me = this;
  121. if (me.pollTask) {
  122. Ext.TaskManager.stop(me.pollTask);
  123. delete me.pollTask;
  124. me.fireEvent('disconnect', me);
  125. }
  126. },
  127. // private
  128. onData: function(opt, success, response){
  129. var me = this,
  130. i = 0,
  131. len,
  132. events;
  133. if (success) {
  134. events = me.createEvents(response);
  135. for (len = events.length; i < len; ++i) {
  136. me.fireEvent('data', me, events[i]);
  137. }
  138. } else {
  139. me.fireEvent('data', me, Ext.create('Ext.direct.ExceptionEvent', {
  140. data: null,
  141. code: Ext.direct.Manager.self.exceptions.TRANSPORT,
  142. message: 'Unable to connect to the server.',
  143. xhr: response
  144. }));
  145. }
  146. }
  147. });