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