3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.data.DirectProxy
\r
9 * @extends Ext.data.DataProxy
\r
11 Ext.data.DirectProxy = function(config){
\r
12 Ext.apply(this, config);
\r
13 if(typeof this.paramOrder == 'string'){
\r
14 this.paramOrder = this.paramOrder.split(/[\s,|]/);
\r
16 Ext.data.DirectProxy.superclass.constructor.call(this, config);
\r
19 Ext.extend(Ext.data.DirectProxy, Ext.data.DataProxy, {
\r
21 * @cfg {Array/String} paramOrder Defaults to <tt>undefined</tt>. A list of params to be executed
\r
22 * server side. Specify the params in the order in which they must be executed on the server-side
\r
23 * as either (1) an Array of String values, or (2) a String of params delimited by either whitespace,
\r
24 * comma, or pipe. For example,
\r
25 * any of the following would be acceptable:<pre><code>
\r
26 paramOrder: ['param1','param2','param3']
\r
27 paramOrder: 'param1 param2 param3'
\r
28 paramOrder: 'param1,param2,param3'
\r
29 paramOrder: 'param1|param2|param'
\r
32 paramOrder: undefined,
\r
35 * @cfg {Boolean} paramsAsHash
\r
36 * Send parameters as a collection of named arguments (defaults to <tt>true</tt>). Providing a
\r
37 * <tt>{@link #paramOrder}</tt> nullifies this configuration.
\r
42 * @cfg {Function} directFn
\r
43 * Function to call when executing a request. directFn is a simple alternative to defining the api configuration-parameter
\r
44 * for Store's which will not implement a full CRUD api.
\r
46 directFn : undefined,
\r
49 doRequest : function(action, rs, params, reader, callback, scope, options) {
\r
51 var directFn = this.api[action] || this.directFn;
\r
54 case Ext.data.Api.actions.create:
\r
55 args.push(params.jsonData[reader.meta.root]); // <-- create(Hash)
\r
57 case Ext.data.Api.actions.read:
\r
58 // If the method has no parameters, ignore the paramOrder/paramsAsHash.
\r
59 if(directFn.directCfg.method.len > 0){
\r
60 if(this.paramOrder){
\r
61 for(var i = 0, len = this.paramOrder.length; i < len; i++){
\r
62 args.push(params[this.paramOrder[i]]);
\r
64 }else if(this.paramsAsHash){
\r
69 case Ext.data.Api.actions.update:
\r
70 args.push(params.jsonData[reader.meta.root]); // <-- update(Hash/Hash[])
\r
72 case Ext.data.Api.actions.destroy:
\r
73 args.push(params.jsonData[reader.meta.root]); // <-- destroy(Int/Int[])
\r
78 params : params || {},
\r
80 callback : callback,
\r
87 args.push(this.createCallback(action, rs, trans), this);
\r
88 directFn.apply(window, args);
\r
92 createCallback : function(action, rs, trans) {
\r
93 return function(result, res) {
\r
95 // @deprecated fire loadexception
\r
96 if (action === Ext.data.Api.actions.read) {
\r
97 this.fireEvent("loadexception", this, trans, res, null);
\r
99 this.fireEvent('exception', this, 'remote', action, trans, res, null);
\r
100 trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);
\r
103 if (action === Ext.data.Api.actions.read) {
\r
104 this.onRead(action, trans, result, res);
\r
106 this.onWrite(action, trans, result, res, rs);
\r
111 * Callback for read actions
\r
112 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
\r
113 * @param {Object} trans The request transaction object
\r
114 * @param {Object} res The server response
\r
117 onRead : function(action, trans, result, res) {
\r
120 records = trans.reader.readRecords(result);
\r
123 // @deprecated: Fire old loadexception for backwards-compat.
\r
124 this.fireEvent("loadexception", this, trans, res, ex);
\r
126 this.fireEvent('exception', this, 'response', action, trans, res, ex);
\r
127 trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);
\r
130 this.fireEvent("load", this, res, trans.request.arg);
\r
131 trans.request.callback.call(trans.request.scope, records, trans.request.arg, true);
\r
134 * Callback for write actions
\r
135 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
\r
136 * @param {Object} trans The request transaction object
\r
137 * @param {Object} res The server response
\r
140 onWrite : function(action, trans, result, res, rs) {
\r
141 var data = trans.reader.extractData(result);
\r
142 this.fireEvent("write", this, action, data, res, rs, trans.request.arg);
\r
143 trans.request.callback.call(trans.request.scope, data, res, true);
\r
148 * @class Ext.data.DirectStore
\r
149 * @extends Ext.data.Store
\r
150 * <p>Small helper class to create an {@link Ext.data.Store} configured with an
\r
151 * {@link Ext.data.DirectProxy} and {@link Ext.data.JsonReader} to make interacting
\r
152 * with an {@link Ext.Direct} Server-side {@link Ext.direct.Provider Provider} easier.
\r
153 * To create a different proxy/reader combination create a basic {@link Ext.data.Store}
\r
154 * configured as needed.</p>
\r
156 * <p><b>*Note:</b> Although they are not listed, this class inherits all of the config options of:</p>
\r
157 * <div><ul class="mdetail-params">
\r
158 * <li><b>{@link Ext.data.Store Store}</b></li>
\r
159 * <div class="sub-desc"><ul class="mdetail-params">
\r
162 * <li><b>{@link Ext.data.JsonReader JsonReader}</b></li>
\r
163 * <div class="sub-desc"><ul class="mdetail-params">
\r
164 * <li><tt><b>{@link Ext.data.JsonReader#root root}</b></tt></li>
\r
165 * <li><tt><b>{@link Ext.data.JsonReader#idProperty idProperty}</b></tt></li>
\r
166 * <li><tt><b>{@link Ext.data.JsonReader#totalProperty totalProperty}</b></tt></li>
\r
169 * <li><b>{@link Ext.data.DirectProxy DirectProxy}</b></li>
\r
170 * <div class="sub-desc"><ul class="mdetail-params">
\r
171 * <li><tt><b>{@link Ext.data.DirectProxy#directFn directFn}</b></tt></li>
\r
172 * <li><tt><b>{@link Ext.data.DirectProxy#paramOrder paramOrder}</b></tt></li>
\r
173 * <li><tt><b>{@link Ext.data.DirectProxy#paramsAsHash paramsAsHash}</b></tt></li>
\r
177 * @xtype directstore
\r
180 * @param {Object} config
\r
182 Ext.data.DirectStore = function(c){
\r
183 // each transaction upon a singe record will generatie a distinct Direct transaction since Direct queues them into one Ajax request.
\r
184 c.batchTransactions = false;
\r
186 Ext.data.DirectStore.superclass.constructor.call(this, Ext.apply(c, {
\r
187 proxy: (typeof(c.proxy) == 'undefined') ? new Ext.data.DirectProxy(Ext.copyTo({}, c, 'paramOrder,paramsAsHash,directFn,api')) : c.proxy,
\r
188 reader: (typeof(c.reader) == 'undefined' && typeof(c.fields) == 'object') ? new Ext.data.JsonReader(Ext.copyTo({}, c, 'totalProperty,root,idProperty'), c.fields) : c.reader
\r
191 Ext.extend(Ext.data.DirectStore, Ext.data.Store, {});
\r
192 Ext.reg('directstore', Ext.data.DirectStore);
194 * @class Ext.Direct
\r
195 * @extends Ext.util.Observable
\r
196 * <p><b><u>Overview</u></b></p>
\r
198 * <p>Ext.Direct aims to streamline communication between the client and server
\r
199 * by providing a single interface that reduces the amount of common code
\r
200 * typically required to validate data and handle returned data packets
\r
201 * (reading data, error conditions, etc).</p>
\r
203 * <p>The Ext.direct namespace includes several classes for a closer integration
\r
204 * with the server-side. The Ext.data namespace also includes classes for working
\r
205 * with Ext.data.Stores which are backed by data from an Ext.Direct method.</p>
\r
207 * <p><b><u>Specification</u></b></p>
\r
209 * <p>For additional information consult the
\r
210 * <a href="http://extjs.com/products/extjs/direct.php">Ext.Direct Specification</a>.</p>
\r
212 * <p><b><u>Providers</u></b></p>
\r
214 * <p>Ext.Direct uses a provider architecture, where one or more providers are
\r
215 * used to transport data to and from the server. There are several providers
\r
216 * that exist in the core at the moment:</p><div class="mdetail-params"><ul>
\r
218 * <li>{@link Ext.direct.JsonProvider JsonProvider} for simple JSON operations</li>
\r
219 * <li>{@link Ext.direct.PollingProvider PollingProvider} for repeated requests</li>
\r
220 * <li>{@link Ext.direct.RemotingProvider RemotingProvider} exposes server side
\r
221 * on the client.</li>
\r
224 * <p>A provider does not need to be invoked directly, providers are added via
\r
225 * {@link Ext.Direct}.{@link Ext.Direct#add add}.</p>
\r
227 * <p><b><u>Router</u></b></p>
\r
229 * <p>Ext.Direct utilizes a "router" on the server to direct requests from the client
\r
230 * to the appropriate server-side method. Because the Ext.Direct API is completely
\r
231 * platform-agnostic, you could completely swap out a Java based server solution
\r
232 * and replace it with one that uses C# without changing the client side JavaScript
\r
235 * <p><b><u>Server side events</u></b></p>
\r
237 * <p>Custom events from the server may be handled by the client by adding
\r
238 * listeners, for example:</p>
\r
240 {"type":"event","name":"message","data":"Successfully polled at: 11:19:30 am"}
\r
242 // add a handler for a 'message' event sent by the server
\r
243 Ext.Direct.on('message', function(e){
\r
244 out.append(String.format('<p><i>{0}</i></p>', e.data));
\r
245 out.el.scrollTo('t', 100000, true);
\r
250 Ext.Direct = Ext.extend(Ext.util.Observable, {
\r
252 * Each event type implements a getData() method. The default event types are:
\r
253 * <div class="mdetail-params"><ul>
\r
254 * <li><b><tt>event</tt></b> : Ext.Direct.Event</li>
\r
255 * <li><b><tt>exception</tt></b> : Ext.Direct.ExceptionEvent</li>
\r
256 * <li><b><tt>rpc</tt></b> : Ext.Direct.RemotingEvent</li>
\r
258 * @property eventTypes
\r
263 * Four types of possible exceptions which can occur:
\r
264 * <div class="mdetail-params"><ul>
\r
265 * <li><b><tt>Ext.Direct.exceptions.TRANSPORT</tt></b> : 'xhr'</li>
\r
266 * <li><b><tt>Ext.Direct.exceptions.PARSE</tt></b> : 'parse'</li>
\r
267 * <li><b><tt>Ext.Direct.exceptions.LOGIN</tt></b> : 'login'</li>
\r
268 * <li><b><tt>Ext.Direct.exceptions.SERVER</tt></b> : 'exception'</li>
\r
270 * @property exceptions
\r
277 SERVER: 'exception'
\r
281 constructor: function(){
\r
285 * Fires after an event.
\r
286 * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.
\r
287 * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.
\r
292 * Fires after an event exception.
\r
293 * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.
\r
297 this.transactions = {};
\r
298 this.providers = {};
\r
302 * Adds an Ext.Direct Provider and creates the proxy or stub methods to execute server-side methods.
\r
303 * If the provider is not already connected, it will auto-connect.
\r
305 var pollProv = new Ext.direct.PollingProvider({
\r
306 url: 'php/poll2.php'
\r
309 Ext.Direct.addProvider(
\r
311 "type":"remoting", // create a {@link Ext.direct.RemotingProvider}
\r
312 "url":"php\/router.php", // url to connect to the Ext.Direct server-side router.
\r
313 "actions":{ // each property within the actions object represents a Class
\r
314 "TestAction":[ // array of methods within each server side Class
\r
316 "name":"doEcho", // name of method
\r
323 "formHandler":true, // handle form on server with Ext.Direct.Transaction
\r
327 "namespace":"myApplication",// namespace to create the Remoting Provider in
\r
329 type: 'polling', // create a {@link Ext.direct.PollingProvider}
\r
330 url: 'php/poll.php'
\r
332 pollProv // reference to previously created instance
\r
335 * @param {Object/Array} provider Accepts either an Array of Provider descriptions (an instance
\r
336 * or config object for a Provider) or any number of Provider descriptions as arguments. Each
\r
337 * Provider description instructs Ext.Direct how to create client-side stub methods.
\r
339 addProvider : function(provider){
\r
342 for(var i = 0, len = a.length; i < len; i++){
\r
343 this.addProvider(a[i]);
\r
348 // if provider has not already been instantiated
\r
349 if(!provider.events){
\r
350 provider = new Ext.Direct.PROVIDERS[provider.type](provider);
\r
352 provider.id = provider.id || Ext.id();
\r
353 this.providers[provider.id] = provider;
\r
355 provider.on('data', this.onProviderData, this);
\r
356 provider.on('exception', this.onProviderException, this);
\r
359 if(!provider.isConnected()){
\r
360 provider.connect();
\r
367 * Retrieve a {@link Ext.direct.Provider provider} by the
\r
368 * <b><tt>{@link Ext.direct.Provider#id id}</tt></b> specified when the provider is
\r
369 * {@link #addProvider added}.
\r
370 * @param {String} id Unique identifier assigned to the provider when calling {@link #addProvider}
\r
372 getProvider : function(id){
\r
373 return this.providers[id];
\r
376 removeProvider : function(id){
\r
377 var provider = id.id ? id : this.providers[id.id];
\r
378 provider.un('data', this.onProviderData, this);
\r
379 provider.un('exception', this.onProviderException, this);
\r
380 delete this.providers[provider.id];
\r
384 addTransaction: function(t){
\r
385 this.transactions[t.tid] = t;
\r
389 removeTransaction: function(t){
\r
390 delete this.transactions[t.tid || t];
\r
394 getTransaction: function(tid){
\r
395 return this.transactions[tid.tid || tid];
\r
398 onProviderData : function(provider, e){
\r
399 if(Ext.isArray(e)){
\r
400 for(var i = 0, len = e.length; i < len; i++){
\r
401 this.onProviderData(provider, e[i]);
\r
405 if(e.name && e.name != 'event' && e.name != 'exception'){
\r
406 this.fireEvent(e.name, e);
\r
407 }else if(e.type == 'exception'){
\r
408 this.fireEvent('exception', e);
\r
410 this.fireEvent('event', e, provider);
\r
413 createEvent : function(response, extraProps){
\r
414 return new Ext.Direct.eventTypes[response.type](Ext.apply(response, extraProps));
\r
417 // overwrite impl. with static instance
\r
418 Ext.Direct = new Ext.Direct();
\r
420 Ext.Direct.TID = 1;
\r
421 Ext.Direct.PROVIDERS = {};/**
\r
422 * @class Ext.Direct.Transaction
\r
424 * <p>Supporting Class for Ext.Direct (not intended to be used directly).</p>
\r
426 * @param {Object} config
\r
428 Ext.Direct.Transaction = function(config){
\r
429 Ext.apply(this, config);
\r
430 this.tid = ++Ext.Direct.TID;
\r
431 this.retryCount = 0;
\r
433 Ext.Direct.Transaction.prototype = {
\r
435 this.provider.queueTransaction(this);
\r
443 getProvider: function(){
\r
444 return this.provider;
\r
446 };Ext.Direct.Event = function(config){
\r
447 Ext.apply(this, config);
\r
449 Ext.Direct.Event.prototype = {
\r
451 getData: function(){
\r
456 Ext.Direct.RemotingEvent = Ext.extend(Ext.Direct.Event, {
\r
458 getTransaction: function(){
\r
459 return this.transaction || Ext.Direct.getTransaction(this.tid);
\r
463 Ext.Direct.ExceptionEvent = Ext.extend(Ext.Direct.RemotingEvent, {
\r
468 Ext.Direct.eventTypes = {
\r
469 'rpc': Ext.Direct.RemotingEvent,
\r
470 'event': Ext.Direct.Event,
\r
471 'exception': Ext.Direct.ExceptionEvent
\r
475 * @class Ext.direct.Provider
\r
476 * @extends Ext.util.Observable
\r
477 * <p>Ext.direct.Provider is an abstract class meant to be extended.</p>
\r
479 * <p>For example ExtJs implements the following subclasses:</p>
\r
483 +---{@link Ext.direct.JsonProvider JsonProvider}
\r
485 +---{@link Ext.direct.PollingProvider PollingProvider}
\r
487 +---{@link Ext.direct.RemotingProvider RemotingProvider}
\r
491 Ext.direct.Provider = Ext.extend(Ext.util.Observable, {
\r
494 * The unique id of the provider (defaults to an {@link Ext#id auto-assigned id}).
\r
495 * You should assign an id if you need to be able to access the provider later and you do
\r
496 * not have an object reference available, for example:
\r
498 Ext.Direct.addProvider(
\r
501 url: 'php/poll.php',
\r
502 id: 'poll-provider'
\r
506 var p = {@link Ext.Direct Ext.Direct}.{@link Ext.Direct#getProvider getProvider}('poll-provider');
\r
512 * @cfg {Number} priority
\r
513 * Priority of the request. Lower is higher priority, <tt>0</tt> means "duplex" (always on).
\r
514 * All Providers default to <tt>1</tt> except for PollingProvider which defaults to <tt>3</tt>.
\r
519 * @cfg {String} type
\r
520 * <b>Required</b>, <tt>undefined</tt> by default. The <tt>type</tt> of provider specified
\r
521 * to {@link Ext.Direct Ext.Direct}.{@link Ext.Direct#addProvider addProvider} to create a
\r
522 * new Provider. Acceptable values by default are:<div class="mdetail-params"><ul>
\r
523 * <li><b><tt>polling</tt></b> : {@link Ext.direct.PollingProvider PollingProvider}</li>
\r
524 * <li><b><tt>remoting</tt></b> : {@link Ext.direct.RemotingProvider RemotingProvider}</li>
\r
529 constructor : function(config){
\r
530 Ext.apply(this, config);
\r
534 * Fires when the Provider connects to the server-side
\r
535 * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.
\r
539 * @event disconnect
\r
540 * Fires when the Provider disconnects from the server-side
\r
541 * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.
\r
546 * Fires when the Provider receives data from the server-side
\r
547 * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.
\r
548 * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.
\r
553 * Fires when the Provider receives an exception from the server-side
\r
557 Ext.direct.Provider.superclass.constructor.call(this, config);
\r
561 * Returns whether or not the server-side is currently connected.
\r
562 * Abstract method for subclasses to implement.
\r
564 isConnected: function(){
\r
569 * Abstract methods for subclasses to implement.
\r
571 connect: Ext.emptyFn,
\r
574 * Abstract methods for subclasses to implement.
\r
576 disconnect: Ext.emptyFn
\r
579 * @class Ext.direct.JsonProvider
\r
580 * @extends Ext.direct.Provider
\r
582 Ext.direct.JsonProvider = Ext.extend(Ext.direct.Provider, {
\r
583 parseResponse: function(xhr){
\r
584 if(!Ext.isEmpty(xhr.responseText)){
\r
585 if(typeof xhr.responseText == 'object'){
\r
586 return xhr.responseText;
\r
588 return Ext.decode(xhr.responseText);
\r
593 getEvents: function(xhr){
\r
596 data = this.parseResponse(xhr);
\r
598 var event = new Ext.Direct.ExceptionEvent({
\r
601 code: Ext.Direct.exceptions.PARSE,
\r
602 message: 'Error parsing json response: \n\n ' + data
\r
607 if(Ext.isArray(data)){
\r
608 for(var i = 0, len = data.length; i < len; i++){
\r
609 events.push(Ext.Direct.createEvent(data[i]));
\r
612 events.push(Ext.Direct.createEvent(data));
\r
617 * @class Ext.direct.PollingProvider
\r
618 * @extends Ext.direct.JsonProvider
\r
620 * <p>Provides for repetitive polling of the server at distinct {@link #interval intervals}.
\r
621 * The initial request for data originates from the client, and then is responded to by the
\r
624 * <p>All configurations for the PollingProvider should be generated by the server-side
\r
625 * API portion of the Ext.Direct stack.</p>
\r
627 * <p>An instance of PollingProvider may be created directly via the new keyword or by simply
\r
628 * specifying <tt>type = 'polling'</tt>. For example:</p>
\r
630 var pollA = new Ext.direct.PollingProvider({
\r
632 url: 'php/pollA.php',
\r
634 Ext.Direct.addProvider(pollA);
\r
635 pollA.disconnect();
\r
637 Ext.Direct.addProvider(
\r
640 url: 'php/pollB.php',
\r
641 id: 'pollB-provider'
\r
644 var pollB = Ext.Direct.getProvider('pollB-provider');
\r
647 Ext.direct.PollingProvider = Ext.extend(Ext.direct.JsonProvider, {
\r
649 * @cfg {Number} priority
\r
650 * Priority of the request (defaults to <tt>3</tt>). See {@link Ext.direct.Provider#priority}.
\r
652 // override default priority
\r
656 * @cfg {Number} interval
\r
657 * How often to poll the server-side in milliseconds (defaults to <tt>3000</tt> - every
\r
663 * @cfg {Object} baseParams An object containing properties which are to be sent as parameters
\r
664 * on every polling request
\r
668 * @cfg {String/Function} url
\r
669 * The url which the PollingProvider should contact with each request. This can also be
\r
670 * an imported Ext.Direct method which will accept the baseParams as its only argument.
\r
674 constructor : function(config){
\r
675 Ext.direct.PollingProvider.superclass.constructor.call(this, config);
\r
678 * @event beforepoll
\r
679 * Fired immediately before a poll takes place, an event handler can return false
\r
680 * in order to cancel the poll.
\r
681 * @param {Ext.direct.PollingProvider}
\r
686 * This event has not yet been implemented.
\r
687 * @param {Ext.direct.PollingProvider}
\r
694 isConnected: function(){
\r
695 return !!this.pollTask;
\r
699 * Connect to the server-side and begin the polling process. To handle each
\r
700 * response subscribe to the data event.
\r
702 connect: function(){
\r
703 if(this.url && !this.pollTask){
\r
704 this.pollTask = Ext.TaskMgr.start({
\r
706 if(this.fireEvent('beforepoll', this) !== false){
\r
707 if(typeof this.url == 'function'){
\r
708 this.url(this.baseParams);
\r
712 callback: this.onData,
\r
714 params: this.baseParams
\r
719 interval: this.interval,
\r
722 this.fireEvent('connect', this);
\r
723 }else if(!this.url){
\r
724 throw 'Error initializing PollingProvider, no url configured.';
\r
729 * Disconnect from the server-side and stop the polling process. The disconnect
\r
730 * event will be fired on a successful disconnect.
\r
732 disconnect: function(){
\r
734 Ext.TaskMgr.stop(this.pollTask);
\r
735 delete this.pollTask;
\r
736 this.fireEvent('disconnect', this);
\r
741 onData: function(opt, success, xhr){
\r
743 var events = this.getEvents(xhr);
\r
744 for(var i = 0, len = events.length; i < len; i++){
\r
746 this.fireEvent('data', this, e);
\r
749 var e = new Ext.Direct.ExceptionEvent({
\r
751 code: Ext.Direct.exceptions.TRANSPORT,
\r
752 message: 'Unable to connect to the server.',
\r
755 this.fireEvent('data', this, e);
\r
760 Ext.Direct.PROVIDERS['polling'] = Ext.direct.PollingProvider;/**
\r
761 * @class Ext.direct.RemotingProvider
\r
762 * @extends Ext.direct.JsonProvider
\r
764 * <p>The {@link Ext.direct.RemotingProvider RemotingProvider} exposes access to
\r
765 * server side methods on the client (a remote procedure call (RPC) type of
\r
766 * connection where the client can initiate a procedure on the server).</p>
\r
768 * <p>This allows for code to be organized in a fashion that is maintainable,
\r
769 * while providing a clear path between client and server, something that is
\r
770 * not always apparent when using URLs.</p>
\r
772 * <p>To accomplish this the server-side needs to describe what classes and methods
\r
773 * are available on the client-side. This configuration will typically be
\r
774 * outputted by the server-side Ext.Direct stack when the API description is built.</p>
\r
776 Ext.direct.RemotingProvider = Ext.extend(Ext.direct.JsonProvider, {
\r
778 * @cfg {Object} actions
\r
779 * Object literal defining the server side actions and methods. For example, if
\r
780 * the Provider is configured with:
\r
782 "actions":{ // each property within the 'actions' object represents a server side Class
\r
783 "TestAction":[ // array of methods within each server side Class to be
\r
784 { // stubbed out on client
\r
788 "name":"multiply",// name of method
\r
789 "len":2 // The number of parameters that will be used to create an
\r
790 // array of data to send to the server side function.
\r
791 // Ensure the server sends back a Number, not a String.
\r
794 "formHandler":true, // direct the client to use specialized form handling method
\r
799 * <p>Note that a Store is not required, a server method can be called at any time.
\r
800 * In the following example a <b>client side</b> handler is used to call the
\r
801 * server side method "multiply" in the server-side "TestAction" Class:</p>
\r
803 TestAction.multiply(
\r
804 2, 4, // pass two arguments to server, so specify len=2
\r
805 // callback function after the server is called
\r
806 // result: the result returned by the server
\r
807 // e: Ext.Direct.RemotingEvent object
\r
808 function(result, e){
\r
809 var t = e.getTransaction();
\r
810 var action = t.action; // server side Class called
\r
811 var method = t.method; // server side method called
\r
813 var answer = Ext.encode(result); // 8
\r
816 var msg = e.message; // failure message
\r
821 * In the example above, the server side "multiply" function will be passed two
\r
822 * arguments (2 and 4). The "multiply" method should return the value 8 which will be
\r
823 * available as the <tt>result</tt> in the example above.
\r
827 * @cfg {String/Object} namespace
\r
828 * Namespace for the Remoting Provider (defaults to the browser global scope of <i>window</i>).
\r
829 * Explicitly specify the namespace Object, or specify a String to have a
\r
830 * {@link Ext#namespace namespace created} implicitly.
\r
834 * @cfg {String} url
\r
835 * <b>Required<b>. The url to connect to the {@link Ext.Direct} server-side router.
\r
839 * @cfg {String} enableUrlEncode
\r
840 * Specify which param will hold the arguments for the method.
\r
841 * Defaults to <tt>'data'</tt>.
\r
845 * @cfg {Number/Boolean} enableBuffer
\r
846 * <p><tt>true</tt> or <tt>false</tt> to enable or disable combining of method
\r
847 * calls. If a number is specified this is the amount of time in milliseconds
\r
848 * to wait before sending a batched request (defaults to <tt>10</tt>).</p>
\r
849 * <br><p>Calls which are received within the specified timeframe will be
\r
850 * concatenated together and sent in a single request, optimizing the
\r
851 * application by reducing the amount of round trips that have to be made
\r
852 * to the server.</p>
\r
857 * @cfg {Number} maxRetries
\r
858 * Number of times to re-attempt delivery on failure of a call. Defaults to <tt>1</tt>.
\r
863 * @cfg {Number} timeout
\r
864 * The timeout to use for each request. Defaults to <tt>undefined</tt>.
\r
866 timeout: undefined,
\r
868 constructor : function(config){
\r
869 Ext.direct.RemotingProvider.superclass.constructor.call(this, config);
\r
872 * @event beforecall
\r
873 * Fires immediately before the client-side sends off the RPC call.
\r
874 * By returning false from an event handler you can prevent the call from
\r
876 * @param {Ext.direct.RemotingProvider} provider
\r
877 * @param {Ext.Direct.Transaction} transaction
\r
882 * Fires immediately after the request to the server-side is sent. This does
\r
883 * NOT fire after the response has come back from the call.
\r
884 * @param {Ext.direct.RemotingProvider} provider
\r
885 * @param {Ext.Direct.Transaction} transaction
\r
889 this.namespace = (Ext.isString(this.namespace)) ? Ext.ns(this.namespace) : this.namespace || window;
\r
890 this.transactions = {};
\r
891 this.callBuffer = [];
\r
895 initAPI : function(){
\r
896 var o = this.actions;
\r
898 var cls = this.namespace[c] || (this.namespace[c] = {}),
\r
900 for(var i = 0, len = ms.length; i < len; i++){
\r
902 cls[m.name] = this.createMethod(c, m);
\r
908 isConnected: function(){
\r
909 return !!this.connected;
\r
912 connect: function(){
\r
915 this.connected = true;
\r
916 this.fireEvent('connect', this);
\r
917 }else if(!this.url){
\r
918 throw 'Error initializing RemotingProvider, no url configured.';
\r
922 disconnect: function(){
\r
923 if(this.connected){
\r
924 this.connected = false;
\r
925 this.fireEvent('disconnect', this);
\r
929 onData: function(opt, success, xhr){
\r
931 var events = this.getEvents(xhr);
\r
932 for(var i = 0, len = events.length; i < len; i++){
\r
934 t = this.getTransaction(e);
\r
935 this.fireEvent('data', this, e);
\r
937 this.doCallback(t, e, true);
\r
938 Ext.Direct.removeTransaction(t);
\r
942 var ts = [].concat(opt.ts);
\r
943 for(var i = 0, len = ts.length; i < len; i++){
\r
944 var t = this.getTransaction(ts[i]);
\r
945 if(t && t.retryCount < this.maxRetries){
\r
948 var e = new Ext.Direct.ExceptionEvent({
\r
951 code: Ext.Direct.exceptions.TRANSPORT,
\r
952 message: 'Unable to connect to the server.',
\r
955 this.fireEvent('data', this, e);
\r
957 this.doCallback(t, e, false);
\r
958 Ext.Direct.removeTransaction(t);
\r
965 getCallData: function(t){
\r
975 doSend : function(data){
\r
978 callback: this.onData,
\r
981 timeout: this.timeout
\r
984 if(Ext.isArray(data)){
\r
986 for(var i = 0, len = data.length; i < len; i++){
\r
987 callData.push(this.getCallData(data[i]));
\r
990 callData = this.getCallData(data);
\r
993 if(this.enableUrlEncode){
\r
995 params[Ext.isString(this.enableUrlEncode) ? this.enableUrlEncode : 'data'] = Ext.encode(callData);
\r
998 o.jsonData = callData;
\r
1000 Ext.Ajax.request(o);
\r
1003 combineAndSend : function(){
\r
1004 var len = this.callBuffer.length;
\r
1006 this.doSend(len == 1 ? this.callBuffer[0] : this.callBuffer);
\r
1007 this.callBuffer = [];
\r
1011 queueTransaction: function(t){
\r
1013 this.processForm(t);
\r
1016 this.callBuffer.push(t);
\r
1017 if(this.enableBuffer){
\r
1018 if(!this.callTask){
\r
1019 this.callTask = new Ext.util.DelayedTask(this.combineAndSend, this);
\r
1021 this.callTask.delay(Ext.isNumber(this.enableBuffer) ? this.enableBuffer : 10);
\r
1023 this.combineAndSend();
\r
1027 doCall : function(c, m, args){
\r
1028 var data = null, hs = args[m.len], scope = args[m.len+1];
\r
1031 data = args.slice(0, m.len);
\r
1034 var t = new Ext.Direct.Transaction({
\r
1040 cb: scope && Ext.isFunction(hs) ? hs.createDelegate(scope) : hs
\r
1043 if(this.fireEvent('beforecall', this, t) !== false){
\r
1044 Ext.Direct.addTransaction(t);
\r
1045 this.queueTransaction(t);
\r
1046 this.fireEvent('call', this, t);
\r
1050 doForm : function(c, m, form, callback, scope){
\r
1051 var t = new Ext.Direct.Transaction({
\r
1055 args:[form, callback, scope],
\r
1056 cb: scope && Ext.isFunction(callback) ? callback.createDelegate(scope) : callback,
\r
1060 if(this.fireEvent('beforecall', this, t) !== false){
\r
1061 Ext.Direct.addTransaction(t);
\r
1062 var isUpload = String(form.getAttribute("enctype")).toLowerCase() == 'multipart/form-data',
\r
1066 extMethod: m.name,
\r
1068 extUpload: String(isUpload)
\r
1071 // change made from typeof callback check to callback.params
\r
1072 // to support addl param passing in DirectSubmit EAC 6/2
\r
1074 form: Ext.getDom(form),
\r
1075 isUpload: isUpload,
\r
1076 params: callback && Ext.isObject(callback.params) ? Ext.apply(params, callback.params) : params
\r
1078 this.fireEvent('call', this, t);
\r
1079 this.processForm(t);
\r
1083 processForm: function(t){
\r
1084 Ext.Ajax.request({
\r
1087 callback: this.onData,
\r
1090 isUpload: t.isUpload,
\r
1095 createMethod : function(c, m){
\r
1097 if(!m.formHandler){
\r
1099 this.doCall(c, m, Array.prototype.slice.call(arguments, 0));
\r
1100 }.createDelegate(this);
\r
1102 f = function(form, callback, scope){
\r
1103 this.doForm(c, m, form, callback, scope);
\r
1104 }.createDelegate(this);
\r
1113 getTransaction: function(opt){
\r
1114 return opt && opt.tid ? Ext.Direct.getTransaction(opt.tid) : null;
\r
1117 doCallback: function(t, e){
\r
1118 var fn = e.status ? 'success' : 'failure';
\r
1121 result = Ext.isDefined(e.result) ? e.result : e.data;
\r
1122 if(Ext.isFunction(hs)){
\r
1125 Ext.callback(hs[fn], hs.scope, [result, e]);
\r
1126 Ext.callback(hs.callback, hs.scope, [result, e]);
\r
1131 Ext.Direct.PROVIDERS['remoting'] = Ext.direct.RemotingProvider;