3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.direct.Manager
17 * <p><b><u>Overview</u></b></p>
19 * <p>Ext.Direct aims to streamline communication between the client and server
20 * by providing a single interface that reduces the amount of common code
21 * typically required to validate data and handle returned data packets
22 * (reading data, error conditions, etc).</p>
24 * <p>The Ext.direct namespace includes several classes for a closer integration
25 * with the server-side. The Ext.data namespace also includes classes for working
26 * with Ext.data.Stores which are backed by data from an Ext.Direct method.</p>
28 * <p><b><u>Specification</u></b></p>
30 * <p>For additional information consult the
31 * <a href="http://sencha.com/products/extjs/extdirect">Ext.Direct Specification</a>.</p>
33 * <p><b><u>Providers</u></b></p>
35 * <p>Ext.Direct uses a provider architecture, where one or more providers are
36 * used to transport data to and from the server. There are several providers
37 * that exist in the core at the moment:</p><div class="mdetail-params"><ul>
39 * <li>{@link Ext.direct.JsonProvider JsonProvider} for simple JSON operations</li>
40 * <li>{@link Ext.direct.PollingProvider PollingProvider} for repeated requests</li>
41 * <li>{@link Ext.direct.RemotingProvider RemotingProvider} exposes server side
45 * <p>A provider does not need to be invoked directly, providers are added via
46 * {@link Ext.direct.Manager}.{@link Ext.direct.Manager#addProvider addProvider}.</p>
48 * <p><b><u>Router</u></b></p>
50 * <p>Ext.Direct utilizes a "router" on the server to direct requests from the client
51 * to the appropriate server-side method. Because the Ext.Direct API is completely
52 * platform-agnostic, you could completely swap out a Java based server solution
53 * and replace it with one that uses C# without changing the client side JavaScript
56 * <p><b><u>Server side events</u></b></p>
58 * <p>Custom events from the server may be handled by the client by adding
59 * listeners, for example:</p>
61 {"type":"event","name":"message","data":"Successfully polled at: 11:19:30 am"}
63 // add a handler for a 'message' event sent by the server
64 Ext.direct.Manager.on('message', function(e){
65 out.append(String.format('<p><i>{0}</i></p>', e.data));
66 out.el.scrollTo('t', 100000, true);
72 Ext.define('Ext.direct.Manager', {
74 /* Begin Definitions */
78 observable: 'Ext.util.Observable'
81 requires: ['Ext.util.MixedCollection'],
94 constructor: function(){
100 * Fires after an event.
101 * @param {event} e The Ext.direct.Event type that occurred.
102 * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.
107 * Fires after an event exception.
108 * @param {event} e The Ext.direct.Event type that occurred.
112 me.transactions = Ext.create('Ext.util.MixedCollection');
113 me.providers = Ext.create('Ext.util.MixedCollection');
115 me.mixins.observable.constructor.call(me);
119 * Adds an Ext.Direct Provider and creates the proxy or stub methods to execute server-side methods.
120 * If the provider is not already connected, it will auto-connect.
122 var pollProv = new Ext.direct.PollingProvider({
126 Ext.direct.Manager.addProvider({
127 "type":"remoting", // create a {@link Ext.direct.RemotingProvider}
128 "url":"php\/router.php", // url to connect to the Ext.Direct server-side router.
129 "actions":{ // each property within the actions object represents a Class
130 "TestAction":[ // array of methods within each server side Class
132 "name":"doEcho", // name of method
139 "formHandler":true, // handle form on server with Ext.Direct.Transaction
143 "namespace":"myApplication",// namespace to create the Remoting Provider in
145 type: 'polling', // create a {@link Ext.direct.PollingProvider}
147 }, pollProv); // reference to previously created instance
149 * @param {Object/Array} provider Accepts either an Array of Provider descriptions (an instance
150 * or config object for a Provider) or any number of Provider descriptions as arguments. Each
151 * Provider description instructs Ext.Direct how to create client-side stub methods.
153 addProvider : function(provider){
159 if (args.length > 1) {
160 for (len = args.length; i < len; ++i) {
161 me.addProvider(args[i]);
166 // if provider has not already been instantiated
167 if (!provider.isProvider) {
168 provider = Ext.create('direct.' + provider.type + 'provider', provider);
170 me.providers.add(provider);
171 provider.on('data', me.onProviderData, me);
174 if (!provider.isConnected()) {
182 * Retrieve a {@link Ext.direct.Provider provider} by the
183 * <b><tt>{@link Ext.direct.Provider#id id}</tt></b> specified when the provider is
184 * {@link #addProvider added}.
185 * @param {String/Ext.data.Provider} id The id of the provider, or the provider instance.
187 getProvider : function(id){
188 return id.isProvider ? id : this.providers.get(id);
192 * Removes the provider.
193 * @param {String/Ext.direct.Provider} provider The provider instance or the id of the provider.
194 * @return {Ext.direct.Provider} The provider, null if not found.
196 removeProvider : function(provider){
198 providers = me.providers,
199 provider = provider.isProvider ? provider : providers.get(provider);
202 provider.un('data', me.onProviderData, me);
203 providers.remove(provider);
210 * Add a transaction to the manager.
212 * @param {Ext.direct.Transaction} transaction The transaction to add
213 * @return {Ext.direct.Transaction} transaction
215 addTransaction: function(transaction){
216 this.transactions.add(transaction);
221 * Remove a transaction from the manager.
223 * @param {String/Ext.direct.Transaction} transaction The transaction/id of transaction to remove
224 * @return {Ext.direct.Transaction} transaction
226 removeTransaction: function(transaction){
227 transaction = this.getTransaction(transaction);
228 this.transactions.remove(transaction);
235 * @param {String/Ext.direct.Transaction} transaction The transaction/id of transaction to get
236 * @return {Ext.direct.Transaction}
238 getTransaction: function(transaction){
239 return transaction.isTransaction ? transaction : this.transactions.get(transaction);
242 onProviderData : function(provider, event){
247 if (Ext.isArray(event)) {
248 for (len = event.length; i < len; ++i) {
249 me.onProviderData(provider, event[i]);
253 if (event.name && event.name != 'event' && event.name != 'exception') {
254 me.fireEvent(event.name, event);
255 } else if (event.type == 'exception') {
256 me.fireEvent('exception', event);
258 me.fireEvent('event', event, provider);
261 // Backwards compatibility
262 Ext.Direct = Ext.direct.Manager;