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
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.Direct"></div>/**
\r
10 * @extends Ext.util.Observable
\r
11 * <p><b><u>Overview</u></b></p>
\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
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
22 * <p><b><u>Specification</u></b></p>
\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
27 * <p><b><u>Providers</u></b></p>
\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
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
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
42 * <p><b><u>Router</u></b></p>
\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
50 * <p><b><u>Server side events</u></b></p>
\r
52 * <p>Custom events from the server may be handled by the client by adding
\r
53 * listeners, for example:</p>
\r
55 {"type":"event","name":"message","data":"Successfully polled at: 11:19:30 am"}
\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('<p><i>{0}</i></p>', e.data));
\r
60 out.el.scrollTo('t', 100000, true);
\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
73 * @property eventTypes
\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
85 * @property exceptions
\r
96 constructor: function(){
\r
98 <div id="event-Ext.Direct-event"></div>/**
\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
105 <div id="event-Ext.Direct-exception"></div>/**
\r
107 * Fires after an event exception.
\r
108 * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.
\r
112 this.transactions = {};
\r
113 this.providers = {};
\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
120 var pollProv = new Ext.direct.PollingProvider({
\r
121 url: 'php/poll2.php'
\r
124 Ext.Direct.addProvider(
\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
131 "name":"doEcho", // name of method
\r
138 "formHandler":true, // handle form on server with Ext.Direct.Transaction
\r
142 "namespace":"myApplication",// namespace to create the Remoting Provider in
\r
144 type: 'polling', // create a {@link Ext.direct.PollingProvider}
\r
145 url: 'php/poll.php'
\r
147 pollProv // reference to previously created instance
\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
154 addProvider : function(provider){
\r
157 for(var i = 0, len = a.length; i < len; i++){
\r
158 this.addProvider(a[i]);
\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
167 provider.id = provider.id || Ext.id();
\r
168 this.providers[provider.id] = provider;
\r
170 provider.on('data', this.onProviderData, this);
\r
171 provider.on('exception', this.onProviderException, this);
\r
174 if(!provider.isConnected()){
\r
175 provider.connect();
\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
187 getProvider : function(id){
\r
188 return this.providers[id];
\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
199 addTransaction: function(t){
\r
200 this.transactions[t.tid] = t;
\r
204 removeTransaction: function(t){
\r
205 delete this.transactions[t.tid || t];
\r
209 getTransaction: function(tid){
\r
210 return this.transactions[tid.tid || tid];
\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
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
225 this.fireEvent('event', e, provider);
\r
228 createEvent : function(response, extraProps){
\r
229 return new Ext.Direct.eventTypes[response.type](Ext.apply(response, extraProps));
\r
232 // overwrite impl. with static instance
\r
233 Ext.Direct = new Ext.Direct();
\r
235 Ext.Direct.TID = 1;
\r
236 Ext.Direct.PROVIDERS = {};</pre>
\r