/javascripts/lib/src/direct/PollingProvider.js

https://bitbucket.org/ksokmesa/sina-asian · JavaScript · 151 lines · 64 code · 9 blank · 78 comment · 9 complexity · 22a08bb81fcb2f4f6c9d0cdcd288cc75 MD5 · raw file

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