3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.2.1
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
15 <div id="cls-Ext.Direct"></div>/**
17 * @extends Ext.util.Observable
18 * <p><b><u>Overview</u></b></p>
20 * <p>Ext.Direct aims to streamline communication between the client and server
21 * by providing a single interface that reduces the amount of common code
22 * typically required to validate data and handle returned data packets
23 * (reading data, error conditions, etc).</p>
25 * <p>The Ext.direct namespace includes several classes for a closer integration
26 * with the server-side. The Ext.data namespace also includes classes for working
27 * with Ext.data.Stores which are backed by data from an Ext.Direct method.</p>
29 * <p><b><u>Specification</u></b></p>
31 * <p>For additional information consult the
32 * <a href="http://extjs.com/products/extjs/direct.php">Ext.Direct Specification</a>.</p>
34 * <p><b><u>Providers</u></b></p>
36 * <p>Ext.Direct uses a provider architecture, where one or more providers are
37 * used to transport data to and from the server. There are several providers
38 * that exist in the core at the moment:</p><div class="mdetail-params"><ul>
40 * <li>{@link Ext.direct.JsonProvider JsonProvider} for simple JSON operations</li>
41 * <li>{@link Ext.direct.PollingProvider PollingProvider} for repeated requests</li>
42 * <li>{@link Ext.direct.RemotingProvider RemotingProvider} exposes server side
46 * <p>A provider does not need to be invoked directly, providers are added via
47 * {@link Ext.Direct}.{@link Ext.Direct#add add}.</p>
49 * <p><b><u>Router</u></b></p>
51 * <p>Ext.Direct utilizes a "router" on the server to direct requests from the client
52 * to the appropriate server-side method. Because the Ext.Direct API is completely
53 * platform-agnostic, you could completely swap out a Java based server solution
54 * and replace it with one that uses C# without changing the client side JavaScript
57 * <p><b><u>Server side events</u></b></p>
59 * <p>Custom events from the server may be handled by the client by adding
60 * listeners, for example:</p>
62 {"type":"event","name":"message","data":"Successfully polled at: 11:19:30 am"}
64 // add a handler for a 'message' event sent by the server
65 Ext.Direct.on('message', function(e){
66 out.append(String.format('<p><i>{0}</i></p>', e.data));
67 out.el.scrollTo('t', 100000, true);
72 Ext.Direct = Ext.extend(Ext.util.Observable, {
73 <div id="prop-Ext.Direct-eventTypes"></div>/**
74 * Each event type implements a getData() method. The default event types are:
75 * <div class="mdetail-params"><ul>
76 * <li><b><tt>event</tt></b> : Ext.Direct.Event</li>
77 * <li><b><tt>exception</tt></b> : Ext.Direct.ExceptionEvent</li>
78 * <li><b><tt>rpc</tt></b> : Ext.Direct.RemotingEvent</li>
80 * @property eventTypes
84 <div id="prop-Ext.Direct-exceptions"></div>/**
85 * Four types of possible exceptions which can occur:
86 * <div class="mdetail-params"><ul>
87 * <li><b><tt>Ext.Direct.exceptions.TRANSPORT</tt></b> : 'xhr'</li>
88 * <li><b><tt>Ext.Direct.exceptions.PARSE</tt></b> : 'parse'</li>
89 * <li><b><tt>Ext.Direct.exceptions.LOGIN</tt></b> : 'login'</li>
90 * <li><b><tt>Ext.Direct.exceptions.SERVER</tt></b> : 'exception'</li>
92 * @property exceptions
103 constructor: function(){
105 <div id="event-Ext.Direct-event"></div>/**
107 * Fires after an event.
108 * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.
109 * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.
112 <div id="event-Ext.Direct-exception"></div>/**
114 * Fires after an event exception.
115 * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.
119 this.transactions = {};
123 <div id="method-Ext.Direct-addProvider"></div>/**
124 * Adds an Ext.Direct Provider and creates the proxy or stub methods to execute server-side methods.
125 * If the provider is not already connected, it will auto-connect.
127 var pollProv = new Ext.direct.PollingProvider({
131 Ext.Direct.addProvider(
133 "type":"remoting", // create a {@link Ext.direct.RemotingProvider}
134 "url":"php\/router.php", // url to connect to the Ext.Direct server-side router.
135 "actions":{ // each property within the actions object represents a Class
136 "TestAction":[ // array of methods within each server side Class
138 "name":"doEcho", // name of method
145 "formHandler":true, // handle form on server with Ext.Direct.Transaction
149 "namespace":"myApplication",// namespace to create the Remoting Provider in
151 type: 'polling', // create a {@link Ext.direct.PollingProvider}
154 pollProv // reference to previously created instance
157 * @param {Object/Array} provider Accepts either an Array of Provider descriptions (an instance
158 * or config object for a Provider) or any number of Provider descriptions as arguments. Each
159 * Provider description instructs Ext.Direct how to create client-side stub methods.
161 addProvider : function(provider){
164 for(var i = 0, len = a.length; i < len; i++){
165 this.addProvider(a[i]);
170 // if provider has not already been instantiated
171 if(!provider.events){
172 provider = new Ext.Direct.PROVIDERS[provider.type](provider);
174 provider.id = provider.id || Ext.id();
175 this.providers[provider.id] = provider;
177 provider.on('data', this.onProviderData, this);
178 provider.on('exception', this.onProviderException, this);
181 if(!provider.isConnected()){
188 <div id="method-Ext.Direct-getProvider"></div>/**
189 * Retrieve a {@link Ext.direct.Provider provider} by the
190 * <b><tt>{@link Ext.direct.Provider#id id}</tt></b> specified when the provider is
191 * {@link #addProvider added}.
192 * @param {String} id Unique identifier assigned to the provider when calling {@link #addProvider}
194 getProvider : function(id){
195 return this.providers[id];
198 removeProvider : function(id){
199 var provider = id.id ? id : this.providers[id];
200 provider.un('data', this.onProviderData, this);
201 provider.un('exception', this.onProviderException, this);
202 delete this.providers[provider.id];
206 addTransaction: function(t){
207 this.transactions[t.tid] = t;
211 removeTransaction: function(t){
212 delete this.transactions[t.tid || t];
216 getTransaction: function(tid){
217 return this.transactions[tid.tid || tid];
220 onProviderData : function(provider, e){
222 for(var i = 0, len = e.length; i < len; i++){
223 this.onProviderData(provider, e[i]);
227 if(e.name && e.name != 'event' && e.name != 'exception'){
228 this.fireEvent(e.name, e);
229 }else if(e.type == 'exception'){
230 this.fireEvent('exception', e);
232 this.fireEvent('event', e, provider);
235 createEvent : function(response, extraProps){
236 return new Ext.Direct.eventTypes[response.type](Ext.apply(response, extraProps));
239 // overwrite impl. with static instance
240 Ext.Direct = new Ext.Direct();
243 Ext.Direct.PROVIDERS = {};</pre>