You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
6.0 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-direct-PollingProvider'>/**
  19. </span> * @class Ext.direct.PollingProvider
  20. * @extends Ext.direct.JsonProvider
  21. *
  22. * &lt;p&gt;Provides for repetitive polling of the server at distinct {@link #interval intervals}.
  23. * The initial request for data originates from the client, and then is responded to by the
  24. * server.&lt;/p&gt;
  25. *
  26. * &lt;p&gt;All configurations for the PollingProvider should be generated by the server-side
  27. * API portion of the Ext.Direct stack.&lt;/p&gt;
  28. *
  29. * &lt;p&gt;An instance of PollingProvider may be created directly via the new keyword or by simply
  30. * specifying &lt;tt&gt;type = 'polling'&lt;/tt&gt;. For example:&lt;/p&gt;
  31. * &lt;pre&gt;&lt;code&gt;
  32. var pollA = new Ext.direct.PollingProvider({
  33. type:'polling',
  34. url: 'php/pollA.php',
  35. });
  36. Ext.Direct.addProvider(pollA);
  37. pollA.disconnect();
  38. Ext.Direct.addProvider(
  39. {
  40. type:'polling',
  41. url: 'php/pollB.php',
  42. id: 'pollB-provider'
  43. }
  44. );
  45. var pollB = Ext.Direct.getProvider('pollB-provider');
  46. * &lt;/code&gt;&lt;/pre&gt;
  47. */
  48. Ext.direct.PollingProvider = Ext.extend(Ext.direct.JsonProvider, {
  49. <span id='Ext-direct-PollingProvider-cfg-priority'> /**
  50. </span> * @cfg {Number} priority
  51. * Priority of the request (defaults to &lt;tt&gt;3&lt;/tt&gt;). See {@link Ext.direct.Provider#priority}.
  52. */
  53. <span id='Ext-direct-PollingProvider-cfg-priority'> // override default priority
  54. </span> priority: 3,
  55. <span id='Ext-direct-PollingProvider-cfg-interval'> /**
  56. </span> * @cfg {Number} interval
  57. * How often to poll the server-side in milliseconds (defaults to &lt;tt&gt;3000&lt;/tt&gt; - every
  58. * 3 seconds).
  59. */
  60. interval: 3000,
  61. <span id='Ext-direct-PollingProvider-cfg-baseParams'> /**
  62. </span> * @cfg {Object} baseParams An object containing properties which are to be sent as parameters
  63. * on every polling request
  64. */
  65. <span id='Ext-direct-PollingProvider-cfg-url'> /**
  66. </span> * @cfg {String/Function} url
  67. * The url which the PollingProvider should contact with each request. This can also be
  68. * an imported Ext.Direct method which will accept the baseParams as its only argument.
  69. */
  70. <span id='Ext-direct-PollingProvider-method-constructor'> // private
  71. </span> constructor : function(config){
  72. Ext.direct.PollingProvider.superclass.constructor.call(this, config);
  73. this.addEvents(
  74. <span id='Ext-direct-PollingProvider-event-beforepoll'> /**
  75. </span> * @event beforepoll
  76. * Fired immediately before a poll takes place, an event handler can return false
  77. * in order to cancel the poll.
  78. * @param {Ext.direct.PollingProvider} this
  79. */
  80. 'beforepoll',
  81. <span id='Ext-direct-PollingProvider-event-poll'> /**
  82. </span> * @event poll
  83. * This event has not yet been implemented.
  84. * @param {Ext.direct.PollingProvider} this
  85. */
  86. 'poll'
  87. );
  88. },
  89. <span id='Ext-direct-PollingProvider-method-isConnected'> // inherited
  90. </span> isConnected: function(){
  91. return !!this.pollTask;
  92. },
  93. <span id='Ext-direct-PollingProvider-method-connect'> /**
  94. </span> * Connect to the server-side and begin the polling process. To handle each
  95. * response subscribe to the data event.
  96. */
  97. connect: function(){
  98. if(this.url &amp;&amp; !this.pollTask){
  99. this.pollTask = Ext.TaskMgr.start({
  100. run: function(){
  101. if(this.fireEvent('beforepoll', this) !== false){
  102. if(typeof this.url == 'function'){
  103. this.url(this.baseParams);
  104. }else{
  105. Ext.Ajax.request({
  106. url: this.url,
  107. callback: this.onData,
  108. scope: this,
  109. params: this.baseParams
  110. });
  111. }
  112. }
  113. },
  114. interval: this.interval,
  115. scope: this
  116. });
  117. this.fireEvent('connect', this);
  118. }else if(!this.url){
  119. throw 'Error initializing PollingProvider, no url configured.';
  120. }
  121. },
  122. <span id='Ext-direct-PollingProvider-method-disconnect'> /**
  123. </span> * Disconnect from the server-side and stop the polling process. The disconnect
  124. * event will be fired on a successful disconnect.
  125. */
  126. disconnect: function(){
  127. if(this.pollTask){
  128. Ext.TaskMgr.stop(this.pollTask);
  129. delete this.pollTask;
  130. this.fireEvent('disconnect', this);
  131. }
  132. },
  133. <span id='Ext-direct-PollingProvider-method-onData'> // private
  134. </span> onData: function(opt, success, xhr){
  135. if(success){
  136. var events = this.getEvents(xhr);
  137. for(var i = 0, len = events.length; i &lt; len; i++){
  138. var e = events[i];
  139. this.fireEvent('data', this, e);
  140. }
  141. }else{
  142. var e = new Ext.Direct.ExceptionEvent({
  143. data: e,
  144. code: Ext.Direct.exceptions.TRANSPORT,
  145. message: 'Unable to connect to the server.',
  146. xhr: xhr
  147. });
  148. this.fireEvent('data', this, e);
  149. }
  150. }
  151. });
  152. Ext.Direct.PROVIDERS['polling'] = Ext.direct.PollingProvider;</pre>
  153. </body>
  154. </html>