PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Framework/ext-4.0.7/src/direct/Manager.js

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