Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / docs / source / Direct.html
1 <html>\r
2 <head>\r
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    \r
4   <title>The source code</title>\r
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
7 </head>\r
8 <body  onload="prettyPrint();">\r
9     <pre class="prettyprint lang-js"><div id="cls-Ext.Direct"></div>/**\r
10  * @class Ext.Direct\r
11  * @extends Ext.util.Observable\r
12  * <p><b><u>Overview</u></b></p>\r
13  * \r
14  * <p>Ext.Direct aims to streamline communication between the client and server\r
15  * by providing a single interface that reduces the amount of common code\r
16  * typically required to validate data and handle returned data packets\r
17  * (reading data, error conditions, etc).</p>\r
18  *  \r
19  * <p>The Ext.direct namespace includes several classes for a closer integration\r
20  * with the server-side. The Ext.data namespace also includes classes for working\r
21  * with Ext.data.Stores which are backed by data from an Ext.Direct method.</p>\r
22  * \r
23  * <p><b><u>Specification</u></b></p>\r
24  * \r
25  * <p>For additional information consult the \r
26  * <a href="http://extjs.com/products/extjs/direct.php">Ext.Direct Specification</a>.</p>\r
27  *   \r
28  * <p><b><u>Providers</u></b></p>\r
29  * \r
30  * <p>Ext.Direct uses a provider architecture, where one or more providers are\r
31  * used to transport data to and from the server. There are several providers\r
32  * that exist in the core at the moment:</p><div class="mdetail-params"><ul>\r
33  * \r
34  * <li>{@link Ext.direct.JsonProvider JsonProvider} for simple JSON operations</li>\r
35  * <li>{@link Ext.direct.PollingProvider PollingProvider} for repeated requests</li>\r
36  * <li>{@link Ext.direct.RemotingProvider RemotingProvider} exposes server side\r
37  * on the client.</li>\r
38  * </ul></div>\r
39  * \r
40  * <p>A provider does not need to be invoked directly, providers are added via\r
41  * {@link Ext.Direct}.{@link Ext.Direct#add add}.</p>\r
42  * \r
43  * <p><b><u>Router</u></b></p>\r
44  * \r
45  * <p>Ext.Direct utilizes a "router" on the server to direct requests from the client\r
46  * to the appropriate server-side method. Because the Ext.Direct API is completely\r
47  * platform-agnostic, you could completely swap out a Java based server solution\r
48  * and replace it with one that uses C# without changing the client side JavaScript\r
49  * at all.</p>\r
50  * \r
51  * <p><b><u>Server side events</u></b></p>\r
52  * \r
53  * <p>Custom events from the server may be handled by the client by adding\r
54  * listeners, for example:</p>\r
55  * <pre><code>\r
56 {"type":"event","name":"message","data":"Successfully polled at: 11:19:30 am"}\r
57 \r
58 // add a handler for a 'message' event sent by the server \r
59 Ext.Direct.on('message', function(e){\r
60     out.append(String.format('&lt;p>&lt;i>{0}&lt;/i>&lt;/p>', e.data));\r
61             out.el.scrollTo('t', 100000, true);\r
62 });\r
63  * </code></pre>\r
64  * @singleton\r
65  */\r
66 Ext.Direct = Ext.extend(Ext.util.Observable, {\r
67     <div id="prop-Ext.Direct-eventTypes"></div>/**\r
68      * Each event type implements a getData() method. The default event types are:\r
69      * <div class="mdetail-params"><ul>\r
70      * <li><b><tt>event</tt></b> : Ext.Direct.Event</li>\r
71      * <li><b><tt>exception</tt></b> : Ext.Direct.ExceptionEvent</li>\r
72      * <li><b><tt>rpc</tt></b> : Ext.Direct.RemotingEvent</li>\r
73      * </ul></div>\r
74      * @property eventTypes\r
75      * @type Object\r
76      */\r
77 \r
78     <div id="prop-Ext.Direct-exceptions"></div>/**\r
79      * Four types of possible exceptions which can occur:\r
80      * <div class="mdetail-params"><ul>\r
81      * <li><b><tt>Ext.Direct.exceptions.TRANSPORT</tt></b> : 'xhr'</li>\r
82      * <li><b><tt>Ext.Direct.exceptions.PARSE</tt></b> : 'parse'</li>\r
83      * <li><b><tt>Ext.Direct.exceptions.LOGIN</tt></b> : 'login'</li>\r
84      * <li><b><tt>Ext.Direct.exceptions.SERVER</tt></b> : 'exception'</li>\r
85      * </ul></div>\r
86      * @property exceptions\r
87      * @type Object\r
88      */\r
89     exceptions: {\r
90         TRANSPORT: 'xhr',\r
91         PARSE: 'parse',\r
92         LOGIN: 'login',\r
93         SERVER: 'exception'\r
94     },\r
95     \r
96     // private\r
97     constructor: function(){\r
98         this.addEvents(\r
99             <div id="event-Ext.Direct-event"></div>/**\r
100              * @event event\r
101              * Fires after an event.\r
102              * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.\r
103              * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.\r
104              */\r
105             'event',\r
106             <div id="event-Ext.Direct-exception"></div>/**\r
107              * @event exception\r
108              * Fires after an event exception.\r
109              * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.\r
110              */\r
111             'exception'\r
112         );\r
113         this.transactions = {};\r
114         this.providers = {};\r
115     },\r
116 \r
117     <div id="method-Ext.Direct-addProvider"></div>/**\r
118      * Adds an Ext.Direct Provider and creates the proxy or stub methods to execute server-side methods.\r
119      * If the provider is not already connected, it will auto-connect.\r
120      * <pre><code>\r
121 var pollProv = new Ext.direct.PollingProvider({\r
122     url: 'php/poll2.php'\r
123 }); \r
124 \r
125 Ext.Direct.addProvider(\r
126     {\r
127         "type":"remoting",       // create a {@link Ext.direct.RemotingProvider} \r
128         "url":"php\/router.php", // url to connect to the Ext.Direct server-side router.\r
129         "actions":{              // each property within the actions object represents a Class \r
130             "TestAction":[       // array of methods within each server side Class   \r
131             {\r
132                 "name":"doEcho", // name of method\r
133                 "len":1\r
134             },{\r
135                 "name":"multiply",\r
136                 "len":1\r
137             },{\r
138                 "name":"doForm",\r
139                 "formHandler":true, // handle form on server with Ext.Direct.Transaction \r
140                 "len":1\r
141             }]\r
142         },\r
143         "namespace":"myApplication",// namespace to create the Remoting Provider in\r
144     },{\r
145         type: 'polling', // create a {@link Ext.direct.PollingProvider} \r
146         url:  'php/poll.php'\r
147     },\r
148     pollProv // reference to previously created instance\r
149 );\r
150      * </code></pre>\r
151      * @param {Object/Array} provider Accepts either an Array of Provider descriptions (an instance\r
152      * or config object for a Provider) or any number of Provider descriptions as arguments.  Each\r
153      * Provider description instructs Ext.Direct how to create client-side stub methods.\r
154      */\r
155     addProvider : function(provider){        \r
156         var a = arguments;\r
157         if(a.length > 1){\r
158             for(var i = 0, len = a.length; i < len; i++){\r
159                 this.addProvider(a[i]);\r
160             }\r
161             return;\r
162         }\r
163         \r
164         // if provider has not already been instantiated\r
165         if(!provider.events){\r
166             provider = new Ext.Direct.PROVIDERS[provider.type](provider);\r
167         }\r
168         provider.id = provider.id || Ext.id();\r
169         this.providers[provider.id] = provider;\r
170 \r
171         provider.on('data', this.onProviderData, this);\r
172         provider.on('exception', this.onProviderException, this);\r
173 \r
174 \r
175         if(!provider.isConnected()){\r
176             provider.connect();\r
177         }\r
178 \r
179         return provider;\r
180     },\r
181 \r
182     <div id="method-Ext.Direct-getProvider"></div>/**\r
183      * Retrieve a {@link Ext.direct.Provider provider} by the\r
184      * <b><tt>{@link Ext.direct.Provider#id id}</tt></b> specified when the provider is\r
185      * {@link #addProvider added}.\r
186      * @param {String} id Unique identifier assigned to the provider when calling {@link #addProvider} \r
187      */\r
188     getProvider : function(id){\r
189         return this.providers[id];\r
190     },\r
191 \r
192     removeProvider : function(id){\r
193         var provider = id.id ? id : this.providers[id.id];\r
194         provider.un('data', this.onProviderData, this);\r
195         provider.un('exception', this.onProviderException, this);\r
196         delete this.providers[provider.id];\r
197         return provider;\r
198     },\r
199 \r
200     addTransaction: function(t){\r
201         this.transactions[t.tid] = t;\r
202         return t;\r
203     },\r
204 \r
205     removeTransaction: function(t){\r
206         delete this.transactions[t.tid || t];\r
207         return t;\r
208     },\r
209 \r
210     getTransaction: function(tid){\r
211         return this.transactions[tid.tid || tid];\r
212     },\r
213 \r
214     onProviderData : function(provider, e){\r
215         if(Ext.isArray(e)){\r
216             for(var i = 0, len = e.length; i < len; i++){\r
217                 this.onProviderData(provider, e[i]);\r
218             }\r
219             return;\r
220         }\r
221         if(e.name && e.name != 'event' && e.name != 'exception'){\r
222             this.fireEvent(e.name, e);\r
223         }else if(e.type == 'exception'){\r
224             this.fireEvent('exception', e);\r
225         }\r
226         this.fireEvent('event', e, provider);\r
227     },\r
228 \r
229     createEvent : function(response, extraProps){\r
230         return new Ext.Direct.eventTypes[response.type](Ext.apply(response, extraProps));\r
231     }\r
232 });\r
233 // overwrite impl. with static instance\r
234 Ext.Direct = new Ext.Direct();\r
235 \r
236 Ext.Direct.TID = 1;\r
237 Ext.Direct.PROVIDERS = {};</pre>    \r
238 </body>\r
239 </html>