Manager2.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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-Manager'>/**
  19. </span> * Ext.Direct aims to streamline communication between the client and server by providing a single interface that
  20. * reduces the amount of common code typically required to validate data and handle returned data packets (reading data,
  21. * error conditions, etc).
  22. *
  23. * The Ext.direct namespace includes several classes for a closer integration with the server-side. The Ext.data
  24. * namespace also includes classes for working with Ext.data.Stores which are backed by data from an Ext.Direct method.
  25. *
  26. * # Specification
  27. *
  28. * For additional information consult the [Ext.Direct Specification][1].
  29. *
  30. * # Providers
  31. *
  32. * Ext.Direct uses a provider architecture, where one or more providers are used to transport data to and from the
  33. * server. There are several providers that exist in the core at the moment:
  34. *
  35. * - {@link Ext.direct.JsonProvider JsonProvider} for simple JSON operations
  36. * - {@link Ext.direct.PollingProvider PollingProvider} for repeated requests
  37. * - {@link Ext.direct.RemotingProvider RemotingProvider} exposes server side on the client.
  38. *
  39. * A provider does not need to be invoked directly, providers are added via {@link Ext.direct.Manager}.{@link #addProvider}.
  40. *
  41. * # Router
  42. *
  43. * Ext.Direct utilizes a &quot;router&quot; on the server to direct requests from the client to the appropriate server-side
  44. * method. Because the Ext.Direct API is completely platform-agnostic, you could completely swap out a Java based server
  45. * solution and replace it with one that uses C# without changing the client side JavaScript at all.
  46. *
  47. * # Server side events
  48. *
  49. * Custom events from the server may be handled by the client by adding listeners, for example:
  50. *
  51. * {&quot;type&quot;:&quot;event&quot;,&quot;name&quot;:&quot;message&quot;,&quot;data&quot;:&quot;Successfully polled at: 11:19:30 am&quot;}
  52. *
  53. * // add a handler for a 'message' event sent by the server
  54. * Ext.direct.Manager.on('message', function(e){
  55. * out.append(String.format('&lt;p&gt;&lt;i&gt;{0}&lt;/i&gt;&lt;/p&gt;', e.data));
  56. * out.el.scrollTo('t', 100000, true);
  57. * });
  58. *
  59. * [1]: http://sencha.com/products/extjs/extdirect
  60. *
  61. * @singleton
  62. * @alternateClassName Ext.Direct
  63. */
  64. Ext.define('Ext.direct.Manager', {
  65. /* Begin Definitions */
  66. singleton: true,
  67. mixins: {
  68. observable: 'Ext.util.Observable'
  69. },
  70. requires: ['Ext.util.MixedCollection'],
  71. <span id='Ext-direct-Manager-property-exceptions'> /**
  72. </span> * Exception types.
  73. */
  74. exceptions: {
  75. TRANSPORT: 'xhr',
  76. PARSE: 'parse',
  77. LOGIN: 'login',
  78. SERVER: 'exception'
  79. },
  80. /* End Definitions */
  81. constructor: function(){
  82. var me = this;
  83. me.addEvents(
  84. <span id='Ext-direct-Manager-event-event'> /**
  85. </span> * @event event
  86. * Fires after an event.
  87. * @param {Ext.direct.Event} e The Ext.direct.Event type that occurred.
  88. * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.
  89. */
  90. 'event',
  91. <span id='Ext-direct-Manager-event-exception'> /**
  92. </span> * @event exception
  93. * Fires after an event exception.
  94. * @param {Ext.direct.Event} e The event type that occurred.
  95. */
  96. 'exception'
  97. );
  98. me.transactions = new Ext.util.MixedCollection();
  99. me.providers = new Ext.util.MixedCollection();
  100. me.mixins.observable.constructor.call(me);
  101. },
  102. <span id='Ext-direct-Manager-method-addProvider'> /**
  103. </span> * Adds an Ext.Direct Provider and creates the proxy or stub methods to execute server-side methods. If the provider
  104. * is not already connected, it will auto-connect.
  105. *
  106. * var pollProv = new Ext.direct.PollingProvider({
  107. * url: 'php/poll2.php'
  108. * });
  109. *
  110. * Ext.direct.Manager.addProvider({
  111. * &quot;type&quot;:&quot;remoting&quot;, // create a {@link Ext.direct.RemotingProvider}
  112. * &quot;url&quot;:&quot;php\/router.php&quot;, // url to connect to the Ext.Direct server-side router.
  113. * &quot;actions&quot;:{ // each property within the actions object represents a Class
  114. * &quot;TestAction&quot;:[ // array of methods within each server side Class
  115. * {
  116. * &quot;name&quot;:&quot;doEcho&quot;, // name of method
  117. * &quot;len&quot;:1
  118. * },{
  119. * &quot;name&quot;:&quot;multiply&quot;,
  120. * &quot;len&quot;:1
  121. * },{
  122. * &quot;name&quot;:&quot;doForm&quot;,
  123. * &quot;formHandler&quot;:true, // handle form on server with Ext.Direct.Transaction
  124. * &quot;len&quot;:1
  125. * }]
  126. * },
  127. * &quot;namespace&quot;:&quot;myApplication&quot;,// namespace to create the Remoting Provider in
  128. * },{
  129. * type: 'polling', // create a {@link Ext.direct.PollingProvider}
  130. * url: 'php/poll.php'
  131. * }, pollProv); // reference to previously created instance
  132. *
  133. * @param {Ext.direct.Provider/Object...} provider
  134. * Accepts any number of Provider descriptions (an instance or config object for
  135. * a Provider). Each Provider description instructs Ext.Directhow to create
  136. * client-side stub methods.
  137. */
  138. addProvider : function(provider){
  139. var me = this,
  140. args = arguments,
  141. i = 0,
  142. len;
  143. if (args.length &gt; 1) {
  144. for (len = args.length; i &lt; len; ++i) {
  145. me.addProvider(args[i]);
  146. }
  147. return;
  148. }
  149. // if provider has not already been instantiated
  150. if (!provider.isProvider) {
  151. provider = Ext.create('direct.' + provider.type + 'provider', provider);
  152. }
  153. me.providers.add(provider);
  154. provider.on('data', me.onProviderData, me);
  155. if (!provider.isConnected()) {
  156. provider.connect();
  157. }
  158. return provider;
  159. },
  160. <span id='Ext-direct-Manager-method-getProvider'> /**
  161. </span> * Retrieves a {@link Ext.direct.Provider provider} by the **{@link Ext.direct.Provider#id id}** specified when the
  162. * provider is {@link #addProvider added}.
  163. * @param {String/Ext.direct.Provider} id The id of the provider, or the provider instance.
  164. */
  165. getProvider : function(id){
  166. return id.isProvider ? id : this.providers.get(id);
  167. },
  168. <span id='Ext-direct-Manager-method-removeProvider'> /**
  169. </span> * Removes the provider.
  170. * @param {String/Ext.direct.Provider} provider The provider instance or the id of the provider.
  171. * @return {Ext.direct.Provider} The provider, null if not found.
  172. */
  173. removeProvider : function(provider){
  174. var me = this,
  175. providers = me.providers;
  176. provider = provider.isProvider ? provider : providers.get(provider);
  177. if (provider) {
  178. provider.un('data', me.onProviderData, me);
  179. providers.remove(provider);
  180. return provider;
  181. }
  182. return null;
  183. },
  184. <span id='Ext-direct-Manager-method-addTransaction'> /**
  185. </span> * Adds a transaction to the manager.
  186. * @private
  187. * @param {Ext.direct.Transaction} transaction The transaction to add
  188. * @return {Ext.direct.Transaction} transaction
  189. */
  190. addTransaction: function(transaction){
  191. this.transactions.add(transaction);
  192. return transaction;
  193. },
  194. <span id='Ext-direct-Manager-method-removeTransaction'> /**
  195. </span> * Removes a transaction from the manager.
  196. * @private
  197. * @param {String/Ext.direct.Transaction} transaction The transaction/id of transaction to remove
  198. * @return {Ext.direct.Transaction} transaction
  199. */
  200. removeTransaction: function(transaction){
  201. transaction = this.getTransaction(transaction);
  202. this.transactions.remove(transaction);
  203. return transaction;
  204. },
  205. <span id='Ext-direct-Manager-method-getTransaction'> /**
  206. </span> * Gets a transaction
  207. * @private
  208. * @param {String/Ext.direct.Transaction} transaction The transaction/id of transaction to get
  209. * @return {Ext.direct.Transaction}
  210. */
  211. getTransaction: function(transaction){
  212. return Ext.isObject(transaction) ? transaction : this.transactions.get(transaction);
  213. },
  214. onProviderData : function(provider, event){
  215. var me = this,
  216. i = 0,
  217. len;
  218. if (Ext.isArray(event)) {
  219. for (len = event.length; i &lt; len; ++i) {
  220. me.onProviderData(provider, event[i]);
  221. }
  222. return;
  223. }
  224. if (event.name &amp;&amp; event.name != 'event' &amp;&amp; event.name != 'exception') {
  225. me.fireEvent(event.name, event);
  226. } else if (event.status === false) {
  227. me.fireEvent('exception', event);
  228. }
  229. me.fireEvent('event', event, provider);
  230. },
  231. <span id='Ext-direct-Manager-method-parseMethod'> /**
  232. </span> * Parses a direct function. It may be passed in a string format, for example:
  233. * &quot;MyApp.Person.read&quot;.
  234. * @protected
  235. * @param {String/Function} fn The direct function
  236. * @return {Function} The function to use in the direct call. Null if not found
  237. */
  238. parseMethod: function(fn){
  239. if (Ext.isString(fn)) {
  240. var parts = fn.split('.'),
  241. i = 0,
  242. len = parts.length,
  243. current = window;
  244. while (current &amp;&amp; i &lt; len) {
  245. current = current[parts[i]];
  246. ++i;
  247. }
  248. fn = Ext.isFunction(current) ? current : null;
  249. }
  250. return fn || null;
  251. }
  252. }, function(){
  253. // Backwards compatibility
  254. Ext.Direct = Ext.direct.Manager;
  255. });
  256. </pre>
  257. </body>
  258. </html>