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 * DirectProxy implementation of {@link Ext.data.DataProxy#doRequest}
\r
50 * @param {String} action The crud action type (create, read, update, destroy)
\r
51 * @param {Ext.data.Record/Ext.data.Record[]} rs If action is load, rs will be null
\r
52 * @param {Object} params An object containing properties which are to be used as HTTP parameters
\r
53 * for the request to the remote server.
\r
54 * @param {Ext.data.DataReader} reader The Reader object which converts the data
\r
55 * object into a block of Ext.data.Records.
\r
56 * @param {Function} callback
\r
57 * <div class="sub-desc"><p>A function to be called after the request.
\r
58 * The <tt>callback</tt> is passed the following arguments:<ul>
\r
59 * <li><tt>r</tt> : Ext.data.Record[] The block of Ext.data.Records.</li>
\r
60 * <li><tt>options</tt>: Options object from the action request</li>
\r
61 * <li><tt>success</tt>: Boolean success indicator</li></ul></p></div>
\r
62 * @param {Object} scope The scope (<code>this</code> reference) in which the callback function is executed. Defaults to the browser window.
\r
63 * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
\r
66 doRequest : function(action, rs, params, reader, callback, scope, options) {
\r
68 directFn = this.api[action] || this.directFn;
\r
71 case Ext.data.Api.actions.create:
\r
72 args.push(params.jsonData); // <-- create(Hash)
\r
74 case Ext.data.Api.actions.read:
\r
75 // If the method has no parameters, ignore the paramOrder/paramsAsHash.
\r
76 if(directFn.directCfg.method.len > 0){
\r
77 if(this.paramOrder){
\r
78 for(var i = 0, len = this.paramOrder.length; i < len; i++){
\r
79 args.push(params[this.paramOrder[i]]);
\r
81 }else if(this.paramsAsHash){
\r
86 case Ext.data.Api.actions.update:
\r
87 args.push(params.jsonData); // <-- update(Hash/Hash[])
\r
89 case Ext.data.Api.actions.destroy:
\r
90 args.push(params.jsonData); // <-- destroy(Int/Int[])
\r
95 params : params || {},
\r
97 callback : callback,
\r
104 args.push(this.createCallback(action, rs, trans), this);
\r
105 directFn.apply(window, args);
\r
109 createCallback : function(action, rs, trans) {
\r
110 return function(result, res) {
\r
112 // @deprecated fire loadexception
\r
113 if (action === Ext.data.Api.actions.read) {
\r
114 this.fireEvent("loadexception", this, trans, res, null);
\r
116 this.fireEvent('exception', this, 'remote', action, trans, res, null);
\r
117 trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);
\r
120 if (action === Ext.data.Api.actions.read) {
\r
121 this.onRead(action, trans, result, res);
\r
123 this.onWrite(action, trans, result, res, rs);
\r
128 * Callback for read actions
\r
129 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
\r
130 * @param {Object} trans The request transaction object
\r
131 * @param {Object} result Data object picked out of the server-response.
\r
132 * @param {Object} res The server response
\r
135 onRead : function(action, trans, result, res) {
\r
138 records = trans.reader.readRecords(result);
\r
141 // @deprecated: Fire old loadexception for backwards-compat.
\r
142 this.fireEvent("loadexception", this, trans, res, ex);
\r
144 this.fireEvent('exception', this, 'response', action, trans, res, ex);
\r
145 trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);
\r
148 this.fireEvent("load", this, res, trans.request.arg);
\r
149 trans.request.callback.call(trans.request.scope, records, trans.request.arg, true);
\r
152 * Callback for write actions
\r
153 * @param {String} action [{@link Ext.data.Api#actions create|read|update|destroy}]
\r
154 * @param {Object} trans The request transaction object
\r
155 * @param {Object} result Data object picked out of the server-response.
\r
156 * @param {Object} res The server response
\r
157 * @param {Ext.data.Record/[Ext.data.Record]} rs The Store resultset associated with the action.
\r
160 onWrite : function(action, trans, result, res, rs) {
\r
161 var data = trans.reader.extractData(result, false);
\r
162 this.fireEvent("write", this, action, data, res, rs, trans.request.arg);
\r
163 trans.request.callback.call(trans.request.scope, data, res, true);
\r
168 * @class Ext.data.DirectStore
\r
169 * @extends Ext.data.Store
\r
170 * <p>Small helper class to create an {@link Ext.data.Store} configured with an
\r
171 * {@link Ext.data.DirectProxy} and {@link Ext.data.JsonReader} to make interacting
\r
172 * with an {@link Ext.Direct} Server-side {@link Ext.direct.Provider Provider} easier.
\r
173 * To create a different proxy/reader combination create a basic {@link Ext.data.Store}
\r
174 * configured as needed.</p>
\r
176 * <p><b>*Note:</b> Although they are not listed, this class inherits all of the config options of:</p>
\r
177 * <div><ul class="mdetail-params">
\r
178 * <li><b>{@link Ext.data.Store Store}</b></li>
\r
179 * <div class="sub-desc"><ul class="mdetail-params">
\r
182 * <li><b>{@link Ext.data.JsonReader JsonReader}</b></li>
\r
183 * <div class="sub-desc"><ul class="mdetail-params">
\r
184 * <li><tt><b>{@link Ext.data.JsonReader#root root}</b></tt></li>
\r
185 * <li><tt><b>{@link Ext.data.JsonReader#idProperty idProperty}</b></tt></li>
\r
186 * <li><tt><b>{@link Ext.data.JsonReader#totalProperty totalProperty}</b></tt></li>
\r
189 * <li><b>{@link Ext.data.DirectProxy DirectProxy}</b></li>
\r
190 * <div class="sub-desc"><ul class="mdetail-params">
\r
191 * <li><tt><b>{@link Ext.data.DirectProxy#directFn directFn}</b></tt></li>
\r
192 * <li><tt><b>{@link Ext.data.DirectProxy#paramOrder paramOrder}</b></tt></li>
\r
193 * <li><tt><b>{@link Ext.data.DirectProxy#paramsAsHash paramsAsHash}</b></tt></li>
\r
197 * @xtype directstore
\r
200 * @param {Object} config
\r
202 Ext.data.DirectStore = function(c){
\r
203 // each transaction upon a singe record will generatie a distinct Direct transaction since Direct queues them into one Ajax request.
\r
204 c.batchTransactions = false;
\r
206 Ext.data.DirectStore.superclass.constructor.call(this, Ext.apply(c, {
\r
207 proxy: (typeof(c.proxy) == 'undefined') ? new Ext.data.DirectProxy(Ext.copyTo({}, c, 'paramOrder,paramsAsHash,directFn,api')) : c.proxy,
\r
208 reader: (typeof(c.reader) == 'undefined' && typeof(c.fields) == 'object') ? new Ext.data.JsonReader(Ext.copyTo({}, c, 'totalProperty,root,idProperty'), c.fields) : c.reader
\r
211 Ext.extend(Ext.data.DirectStore, Ext.data.Store, {});
\r
212 Ext.reg('directstore', Ext.data.DirectStore);
214 * @class Ext.Direct
\r
215 * @extends Ext.util.Observable
\r
216 * <p><b><u>Overview</u></b></p>
\r
218 * <p>Ext.Direct aims to streamline communication between the client and server
\r
219 * by providing a single interface that reduces the amount of common code
\r
220 * typically required to validate data and handle returned data packets
\r
221 * (reading data, error conditions, etc).</p>
\r
223 * <p>The Ext.direct namespace includes several classes for a closer integration
\r
224 * with the server-side. The Ext.data namespace also includes classes for working
\r
225 * with Ext.data.Stores which are backed by data from an Ext.Direct method.</p>
\r
227 * <p><b><u>Specification</u></b></p>
\r
229 * <p>For additional information consult the
\r
230 * <a href="http://extjs.com/products/extjs/direct.php">Ext.Direct Specification</a>.</p>
\r
232 * <p><b><u>Providers</u></b></p>
\r
234 * <p>Ext.Direct uses a provider architecture, where one or more providers are
\r
235 * used to transport data to and from the server. There are several providers
\r
236 * that exist in the core at the moment:</p><div class="mdetail-params"><ul>
\r
238 * <li>{@link Ext.direct.JsonProvider JsonProvider} for simple JSON operations</li>
\r
239 * <li>{@link Ext.direct.PollingProvider PollingProvider} for repeated requests</li>
\r
240 * <li>{@link Ext.direct.RemotingProvider RemotingProvider} exposes server side
\r
241 * on the client.</li>
\r
244 * <p>A provider does not need to be invoked directly, providers are added via
\r
245 * {@link Ext.Direct}.{@link Ext.Direct#add add}.</p>
\r
247 * <p><b><u>Router</u></b></p>
\r
249 * <p>Ext.Direct utilizes a "router" on the server to direct requests from the client
\r
250 * to the appropriate server-side method. Because the Ext.Direct API is completely
\r
251 * platform-agnostic, you could completely swap out a Java based server solution
\r
252 * and replace it with one that uses C# without changing the client side JavaScript
\r
255 * <p><b><u>Server side events</u></b></p>
\r
257 * <p>Custom events from the server may be handled by the client by adding
\r
258 * listeners, for example:</p>
\r
260 {"type":"event","name":"message","data":"Successfully polled at: 11:19:30 am"}
\r
262 // add a handler for a 'message' event sent by the server
\r
263 Ext.Direct.on('message', function(e){
\r
264 out.append(String.format('<p><i>{0}</i></p>', e.data));
\r
265 out.el.scrollTo('t', 100000, true);
\r
270 Ext.Direct = Ext.extend(Ext.util.Observable, {
\r
272 * Each event type implements a getData() method. The default event types are:
\r
273 * <div class="mdetail-params"><ul>
\r
274 * <li><b><tt>event</tt></b> : Ext.Direct.Event</li>
\r
275 * <li><b><tt>exception</tt></b> : Ext.Direct.ExceptionEvent</li>
\r
276 * <li><b><tt>rpc</tt></b> : Ext.Direct.RemotingEvent</li>
\r
278 * @property eventTypes
\r
283 * Four types of possible exceptions which can occur:
\r
284 * <div class="mdetail-params"><ul>
\r
285 * <li><b><tt>Ext.Direct.exceptions.TRANSPORT</tt></b> : 'xhr'</li>
\r
286 * <li><b><tt>Ext.Direct.exceptions.PARSE</tt></b> : 'parse'</li>
\r
287 * <li><b><tt>Ext.Direct.exceptions.LOGIN</tt></b> : 'login'</li>
\r
288 * <li><b><tt>Ext.Direct.exceptions.SERVER</tt></b> : 'exception'</li>
\r
290 * @property exceptions
\r
297 SERVER: 'exception'
\r
301 constructor: function(){
\r
305 * Fires after an event.
\r
306 * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.
\r
307 * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.
\r
312 * Fires after an event exception.
\r
313 * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.
\r
317 this.transactions = {};
\r
318 this.providers = {};
\r
322 * Adds an Ext.Direct Provider and creates the proxy or stub methods to execute server-side methods.
\r
323 * If the provider is not already connected, it will auto-connect.
\r
325 var pollProv = new Ext.direct.PollingProvider({
\r
326 url: 'php/poll2.php'
\r
329 Ext.Direct.addProvider(
\r
331 "type":"remoting", // create a {@link Ext.direct.RemotingProvider}
\r
332 "url":"php\/router.php", // url to connect to the Ext.Direct server-side router.
\r
333 "actions":{ // each property within the actions object represents a Class
\r
334 "TestAction":[ // array of methods within each server side Class
\r
336 "name":"doEcho", // name of method
\r
343 "formHandler":true, // handle form on server with Ext.Direct.Transaction
\r
347 "namespace":"myApplication",// namespace to create the Remoting Provider in
\r
349 type: 'polling', // create a {@link Ext.direct.PollingProvider}
\r
350 url: 'php/poll.php'
\r
352 pollProv // reference to previously created instance
\r
355 * @param {Object/Array} provider Accepts either an Array of Provider descriptions (an instance
\r
356 * or config object for a Provider) or any number of Provider descriptions as arguments. Each
\r
357 * Provider description instructs Ext.Direct how to create client-side stub methods.
\r
359 addProvider : function(provider){
\r
362 for(var i = 0, len = a.length; i < len; i++){
\r
363 this.addProvider(a[i]);
\r
368 // if provider has not already been instantiated
\r
369 if(!provider.events){
\r
370 provider = new Ext.Direct.PROVIDERS[provider.type](provider);
\r
372 provider.id = provider.id || Ext.id();
\r
373 this.providers[provider.id] = provider;
\r
375 provider.on('data', this.onProviderData, this);
\r
376 provider.on('exception', this.onProviderException, this);
\r
379 if(!provider.isConnected()){
\r
380 provider.connect();
\r
387 * Retrieve a {@link Ext.direct.Provider provider} by the
\r
388 * <b><tt>{@link Ext.direct.Provider#id id}</tt></b> specified when the provider is
\r
389 * {@link #addProvider added}.
\r
390 * @param {String} id Unique identifier assigned to the provider when calling {@link #addProvider}
\r
392 getProvider : function(id){
\r
393 return this.providers[id];
\r
396 removeProvider : function(id){
\r
397 var provider = id.id ? id : this.providers[id.id];
\r
398 provider.un('data', this.onProviderData, this);
\r
399 provider.un('exception', this.onProviderException, this);
\r
400 delete this.providers[provider.id];
\r
404 addTransaction: function(t){
\r
405 this.transactions[t.tid] = t;
\r
409 removeTransaction: function(t){
\r
410 delete this.transactions[t.tid || t];
\r
414 getTransaction: function(tid){
\r
415 return this.transactions[tid.tid || tid];
\r
418 onProviderData : function(provider, e){
\r
419 if(Ext.isArray(e)){
\r
420 for(var i = 0, len = e.length; i < len; i++){
\r
421 this.onProviderData(provider, e[i]);
\r
425 if(e.name && e.name != 'event' && e.name != 'exception'){
\r
426 this.fireEvent(e.name, e);
\r
427 }else if(e.type == 'exception'){
\r
428 this.fireEvent('exception', e);
\r
430 this.fireEvent('event', e, provider);
\r
433 createEvent : function(response, extraProps){
\r
434 return new Ext.Direct.eventTypes[response.type](Ext.apply(response, extraProps));
\r
437 // overwrite impl. with static instance
\r
438 Ext.Direct = new Ext.Direct();
\r
440 Ext.Direct.TID = 1;
\r
441 Ext.Direct.PROVIDERS = {};/**
\r
442 * @class Ext.Direct.Transaction
\r
444 * <p>Supporting Class for Ext.Direct (not intended to be used directly).</p>
\r
446 * @param {Object} config
\r
448 Ext.Direct.Transaction = function(config){
\r
449 Ext.apply(this, config);
\r
450 this.tid = ++Ext.Direct.TID;
\r
451 this.retryCount = 0;
\r
453 Ext.Direct.Transaction.prototype = {
\r
455 this.provider.queueTransaction(this);
\r
463 getProvider: function(){
\r
464 return this.provider;
\r
466 };Ext.Direct.Event = function(config){
\r
467 Ext.apply(this, config);
\r
469 Ext.Direct.Event.prototype = {
\r
471 getData: function(){
\r
476 Ext.Direct.RemotingEvent = Ext.extend(Ext.Direct.Event, {
\r
478 getTransaction: function(){
\r
479 return this.transaction || Ext.Direct.getTransaction(this.tid);
\r
483 Ext.Direct.ExceptionEvent = Ext.extend(Ext.Direct.RemotingEvent, {
\r
488 Ext.Direct.eventTypes = {
\r
489 'rpc': Ext.Direct.RemotingEvent,
\r
490 'event': Ext.Direct.Event,
\r
491 'exception': Ext.Direct.ExceptionEvent
\r
495 * @class Ext.direct.Provider
\r
496 * @extends Ext.util.Observable
\r
497 * <p>Ext.direct.Provider is an abstract class meant to be extended.</p>
\r
499 * <p>For example ExtJs implements the following subclasses:</p>
\r
503 +---{@link Ext.direct.JsonProvider JsonProvider}
\r
505 +---{@link Ext.direct.PollingProvider PollingProvider}
\r
507 +---{@link Ext.direct.RemotingProvider RemotingProvider}
\r
511 Ext.direct.Provider = Ext.extend(Ext.util.Observable, {
\r
514 * The unique id of the provider (defaults to an {@link Ext#id auto-assigned id}).
\r
515 * You should assign an id if you need to be able to access the provider later and you do
\r
516 * not have an object reference available, for example:
\r
518 Ext.Direct.addProvider(
\r
521 url: 'php/poll.php',
\r
522 id: 'poll-provider'
\r
526 var p = {@link Ext.Direct Ext.Direct}.{@link Ext.Direct#getProvider getProvider}('poll-provider');
\r
532 * @cfg {Number} priority
\r
533 * Priority of the request. Lower is higher priority, <tt>0</tt> means "duplex" (always on).
\r
534 * All Providers default to <tt>1</tt> except for PollingProvider which defaults to <tt>3</tt>.
\r
539 * @cfg {String} type
\r
540 * <b>Required</b>, <tt>undefined</tt> by default. The <tt>type</tt> of provider specified
\r
541 * to {@link Ext.Direct Ext.Direct}.{@link Ext.Direct#addProvider addProvider} to create a
\r
542 * new Provider. Acceptable values by default are:<div class="mdetail-params"><ul>
\r
543 * <li><b><tt>polling</tt></b> : {@link Ext.direct.PollingProvider PollingProvider}</li>
\r
544 * <li><b><tt>remoting</tt></b> : {@link Ext.direct.RemotingProvider RemotingProvider}</li>
\r
549 constructor : function(config){
\r
550 Ext.apply(this, config);
\r
554 * Fires when the Provider connects to the server-side
\r
555 * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.
\r
559 * @event disconnect
\r
560 * Fires when the Provider disconnects from the server-side
\r
561 * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.
\r
566 * Fires when the Provider receives data from the server-side
\r
567 * @param {Ext.direct.Provider} provider The {@link Ext.direct.Provider Provider}.
\r
568 * @param {event} e The {@link Ext.Direct#eventTypes Ext.Direct.Event type} that occurred.
\r
573 * Fires when the Provider receives an exception from the server-side
\r
577 Ext.direct.Provider.superclass.constructor.call(this, config);
\r
581 * Returns whether or not the server-side is currently connected.
\r
582 * Abstract method for subclasses to implement.
\r
584 isConnected: function(){
\r
589 * Abstract methods for subclasses to implement.
\r
591 connect: Ext.emptyFn,
\r
594 * Abstract methods for subclasses to implement.
\r
596 disconnect: Ext.emptyFn
\r
599 * @class Ext.direct.JsonProvider
\r
600 * @extends Ext.direct.Provider
\r
602 Ext.direct.JsonProvider = Ext.extend(Ext.direct.Provider, {
\r
603 parseResponse: function(xhr){
\r
604 if(!Ext.isEmpty(xhr.responseText)){
\r
605 if(typeof xhr.responseText == 'object'){
\r
606 return xhr.responseText;
\r
608 return Ext.decode(xhr.responseText);
\r
613 getEvents: function(xhr){
\r
616 data = this.parseResponse(xhr);
\r
618 var event = new Ext.Direct.ExceptionEvent({
\r
621 code: Ext.Direct.exceptions.PARSE,
\r
622 message: 'Error parsing json response: \n\n ' + data
\r
627 if(Ext.isArray(data)){
\r
628 for(var i = 0, len = data.length; i < len; i++){
\r
629 events.push(Ext.Direct.createEvent(data[i]));
\r
632 events.push(Ext.Direct.createEvent(data));
\r
637 * @class Ext.direct.PollingProvider
\r
638 * @extends Ext.direct.JsonProvider
\r
640 * <p>Provides for repetitive polling of the server at distinct {@link #interval intervals}.
\r
641 * The initial request for data originates from the client, and then is responded to by the
\r
644 * <p>All configurations for the PollingProvider should be generated by the server-side
\r
645 * API portion of the Ext.Direct stack.</p>
\r
647 * <p>An instance of PollingProvider may be created directly via the new keyword or by simply
\r
648 * specifying <tt>type = 'polling'</tt>. For example:</p>
\r
650 var pollA = new Ext.direct.PollingProvider({
\r
652 url: 'php/pollA.php',
\r
654 Ext.Direct.addProvider(pollA);
\r
655 pollA.disconnect();
\r
657 Ext.Direct.addProvider(
\r
660 url: 'php/pollB.php',
\r
661 id: 'pollB-provider'
\r
664 var pollB = Ext.Direct.getProvider('pollB-provider');
\r
667 Ext.direct.PollingProvider = Ext.extend(Ext.direct.JsonProvider, {
\r
669 * @cfg {Number} priority
\r
670 * Priority of the request (defaults to <tt>3</tt>). See {@link Ext.direct.Provider#priority}.
\r
672 // override default priority
\r
676 * @cfg {Number} interval
\r
677 * How often to poll the server-side in milliseconds (defaults to <tt>3000</tt> - every
\r
683 * @cfg {Object} baseParams An object containing properties which are to be sent as parameters
\r
684 * on every polling request
\r
688 * @cfg {String/Function} url
\r
689 * The url which the PollingProvider should contact with each request. This can also be
\r
690 * an imported Ext.Direct method which will accept the baseParams as its only argument.
\r
694 constructor : function(config){
\r
695 Ext.direct.PollingProvider.superclass.constructor.call(this, config);
\r
698 * @event beforepoll
\r
699 * Fired immediately before a poll takes place, an event handler can return false
\r
700 * in order to cancel the poll.
\r
701 * @param {Ext.direct.PollingProvider}
\r
706 * This event has not yet been implemented.
\r
707 * @param {Ext.direct.PollingProvider}
\r
714 isConnected: function(){
\r
715 return !!this.pollTask;
\r
719 * Connect to the server-side and begin the polling process. To handle each
\r
720 * response subscribe to the data event.
\r
722 connect: function(){
\r
723 if(this.url && !this.pollTask){
\r
724 this.pollTask = Ext.TaskMgr.start({
\r
726 if(this.fireEvent('beforepoll', this) !== false){
\r
727 if(typeof this.url == 'function'){
\r
728 this.url(this.baseParams);
\r
732 callback: this.onData,
\r
734 params: this.baseParams
\r
739 interval: this.interval,
\r
742 this.fireEvent('connect', this);
\r
743 }else if(!this.url){
\r
744 throw 'Error initializing PollingProvider, no url configured.';
\r
749 * Disconnect from the server-side and stop the polling process. The disconnect
\r
750 * event will be fired on a successful disconnect.
\r
752 disconnect: function(){
\r
754 Ext.TaskMgr.stop(this.pollTask);
\r
755 delete this.pollTask;
\r
756 this.fireEvent('disconnect', this);
\r
761 onData: function(opt, success, xhr){
\r
763 var events = this.getEvents(xhr);
\r
764 for(var i = 0, len = events.length; i < len; i++){
\r
766 this.fireEvent('data', this, e);
\r
769 var e = new Ext.Direct.ExceptionEvent({
\r
771 code: Ext.Direct.exceptions.TRANSPORT,
\r
772 message: 'Unable to connect to the server.',
\r
775 this.fireEvent('data', this, e);
\r
780 Ext.Direct.PROVIDERS['polling'] = Ext.direct.PollingProvider;/**
\r
781 * @class Ext.direct.RemotingProvider
\r
782 * @extends Ext.direct.JsonProvider
\r
784 * <p>The {@link Ext.direct.RemotingProvider RemotingProvider} exposes access to
\r
785 * server side methods on the client (a remote procedure call (RPC) type of
\r
786 * connection where the client can initiate a procedure on the server).</p>
\r
788 * <p>This allows for code to be organized in a fashion that is maintainable,
\r
789 * while providing a clear path between client and server, something that is
\r
790 * not always apparent when using URLs.</p>
\r
792 * <p>To accomplish this the server-side needs to describe what classes and methods
\r
793 * are available on the client-side. This configuration will typically be
\r
794 * outputted by the server-side Ext.Direct stack when the API description is built.</p>
\r
796 Ext.direct.RemotingProvider = Ext.extend(Ext.direct.JsonProvider, {
\r
798 * @cfg {Object} actions
\r
799 * Object literal defining the server side actions and methods. For example, if
\r
800 * the Provider is configured with:
\r
802 "actions":{ // each property within the 'actions' object represents a server side Class
\r
803 "TestAction":[ // array of methods within each server side Class to be
\r
804 { // stubbed out on client
\r
808 "name":"multiply",// name of method
\r
809 "len":2 // The number of parameters that will be used to create an
\r
810 // array of data to send to the server side function.
\r
811 // Ensure the server sends back a Number, not a String.
\r
814 "formHandler":true, // direct the client to use specialized form handling method
\r
819 * <p>Note that a Store is not required, a server method can be called at any time.
\r
820 * In the following example a <b>client side</b> handler is used to call the
\r
821 * server side method "multiply" in the server-side "TestAction" Class:</p>
\r
823 TestAction.multiply(
\r
824 2, 4, // pass two arguments to server, so specify len=2
\r
825 // callback function after the server is called
\r
826 // result: the result returned by the server
\r
827 // e: Ext.Direct.RemotingEvent object
\r
828 function(result, e){
\r
829 var t = e.getTransaction();
\r
830 var action = t.action; // server side Class called
\r
831 var method = t.method; // server side method called
\r
833 var answer = Ext.encode(result); // 8
\r
836 var msg = e.message; // failure message
\r
841 * In the example above, the server side "multiply" function will be passed two
\r
842 * arguments (2 and 4). The "multiply" method should return the value 8 which will be
\r
843 * available as the <tt>result</tt> in the example above.
\r
847 * @cfg {String/Object} namespace
\r
848 * Namespace for the Remoting Provider (defaults to the browser global scope of <i>window</i>).
\r
849 * Explicitly specify the namespace Object, or specify a String to have a
\r
850 * {@link Ext#namespace namespace created} implicitly.
\r
854 * @cfg {String} url
\r
855 * <b>Required<b>. The url to connect to the {@link Ext.Direct} server-side router.
\r
859 * @cfg {String} enableUrlEncode
\r
860 * Specify which param will hold the arguments for the method.
\r
861 * Defaults to <tt>'data'</tt>.
\r
865 * @cfg {Number/Boolean} enableBuffer
\r
866 * <p><tt>true</tt> or <tt>false</tt> to enable or disable combining of method
\r
867 * calls. If a number is specified this is the amount of time in milliseconds
\r
868 * to wait before sending a batched request (defaults to <tt>10</tt>).</p>
\r
869 * <br><p>Calls which are received within the specified timeframe will be
\r
870 * concatenated together and sent in a single request, optimizing the
\r
871 * application by reducing the amount of round trips that have to be made
\r
872 * to the server.</p>
\r
877 * @cfg {Number} maxRetries
\r
878 * Number of times to re-attempt delivery on failure of a call. Defaults to <tt>1</tt>.
\r
883 * @cfg {Number} timeout
\r
884 * The timeout to use for each request. Defaults to <tt>undefined</tt>.
\r
886 timeout: undefined,
\r
888 constructor : function(config){
\r
889 Ext.direct.RemotingProvider.superclass.constructor.call(this, config);
\r
892 * @event beforecall
\r
893 * Fires immediately before the client-side sends off the RPC call.
\r
894 * By returning false from an event handler you can prevent the call from
\r
896 * @param {Ext.direct.RemotingProvider} provider
\r
897 * @param {Ext.Direct.Transaction} transaction
\r
902 * Fires immediately after the request to the server-side is sent. This does
\r
903 * NOT fire after the response has come back from the call.
\r
904 * @param {Ext.direct.RemotingProvider} provider
\r
905 * @param {Ext.Direct.Transaction} transaction
\r
909 this.namespace = (Ext.isString(this.namespace)) ? Ext.ns(this.namespace) : this.namespace || window;
\r
910 this.transactions = {};
\r
911 this.callBuffer = [];
\r
915 initAPI : function(){
\r
916 var o = this.actions;
\r
918 var cls = this.namespace[c] || (this.namespace[c] = {}),
\r
920 for(var i = 0, len = ms.length; i < len; i++){
\r
922 cls[m.name] = this.createMethod(c, m);
\r
928 isConnected: function(){
\r
929 return !!this.connected;
\r
932 connect: function(){
\r
935 this.connected = true;
\r
936 this.fireEvent('connect', this);
\r
937 }else if(!this.url){
\r
938 throw 'Error initializing RemotingProvider, no url configured.';
\r
942 disconnect: function(){
\r
943 if(this.connected){
\r
944 this.connected = false;
\r
945 this.fireEvent('disconnect', this);
\r
949 onData: function(opt, success, xhr){
\r
951 var events = this.getEvents(xhr);
\r
952 for(var i = 0, len = events.length; i < len; i++){
\r
954 t = this.getTransaction(e);
\r
955 this.fireEvent('data', this, e);
\r
957 this.doCallback(t, e, true);
\r
958 Ext.Direct.removeTransaction(t);
\r
962 var ts = [].concat(opt.ts);
\r
963 for(var i = 0, len = ts.length; i < len; i++){
\r
964 var t = this.getTransaction(ts[i]);
\r
965 if(t && t.retryCount < this.maxRetries){
\r
968 var e = new Ext.Direct.ExceptionEvent({
\r
971 code: Ext.Direct.exceptions.TRANSPORT,
\r
972 message: 'Unable to connect to the server.',
\r
975 this.fireEvent('data', this, e);
\r
977 this.doCallback(t, e, false);
\r
978 Ext.Direct.removeTransaction(t);
\r
985 getCallData: function(t){
\r
995 doSend : function(data){
\r
998 callback: this.onData,
\r
1001 timeout: this.timeout
\r
1004 if(Ext.isArray(data)){
\r
1006 for(var i = 0, len = data.length; i < len; i++){
\r
1007 callData.push(this.getCallData(data[i]));
\r
1010 callData = this.getCallData(data);
\r
1013 if(this.enableUrlEncode){
\r
1015 params[Ext.isString(this.enableUrlEncode) ? this.enableUrlEncode : 'data'] = Ext.encode(callData);
\r
1016 o.params = params;
\r
1018 o.jsonData = callData;
\r
1020 Ext.Ajax.request(o);
\r
1023 combineAndSend : function(){
\r
1024 var len = this.callBuffer.length;
\r
1026 this.doSend(len == 1 ? this.callBuffer[0] : this.callBuffer);
\r
1027 this.callBuffer = [];
\r
1031 queueTransaction: function(t){
\r
1033 this.processForm(t);
\r
1036 this.callBuffer.push(t);
\r
1037 if(this.enableBuffer){
\r
1038 if(!this.callTask){
\r
1039 this.callTask = new Ext.util.DelayedTask(this.combineAndSend, this);
\r
1041 this.callTask.delay(Ext.isNumber(this.enableBuffer) ? this.enableBuffer : 10);
\r
1043 this.combineAndSend();
\r
1047 doCall : function(c, m, args){
\r
1048 var data = null, hs = args[m.len], scope = args[m.len+1];
\r
1051 data = args.slice(0, m.len);
\r
1054 var t = new Ext.Direct.Transaction({
\r
1060 cb: scope && Ext.isFunction(hs) ? hs.createDelegate(scope) : hs
\r
1063 if(this.fireEvent('beforecall', this, t) !== false){
\r
1064 Ext.Direct.addTransaction(t);
\r
1065 this.queueTransaction(t);
\r
1066 this.fireEvent('call', this, t);
\r
1070 doForm : function(c, m, form, callback, scope){
\r
1071 var t = new Ext.Direct.Transaction({
\r
1075 args:[form, callback, scope],
\r
1076 cb: scope && Ext.isFunction(callback) ? callback.createDelegate(scope) : callback,
\r
1080 if(this.fireEvent('beforecall', this, t) !== false){
\r
1081 Ext.Direct.addTransaction(t);
\r
1082 var isUpload = String(form.getAttribute("enctype")).toLowerCase() == 'multipart/form-data',
\r
1086 extMethod: m.name,
\r
1088 extUpload: String(isUpload)
\r
1091 // change made from typeof callback check to callback.params
\r
1092 // to support addl param passing in DirectSubmit EAC 6/2
\r
1094 form: Ext.getDom(form),
\r
1095 isUpload: isUpload,
\r
1096 params: callback && Ext.isObject(callback.params) ? Ext.apply(params, callback.params) : params
\r
1098 this.fireEvent('call', this, t);
\r
1099 this.processForm(t);
\r
1103 processForm: function(t){
\r
1104 Ext.Ajax.request({
\r
1107 callback: this.onData,
\r
1110 isUpload: t.isUpload,
\r
1115 createMethod : function(c, m){
\r
1117 if(!m.formHandler){
\r
1119 this.doCall(c, m, Array.prototype.slice.call(arguments, 0));
\r
1120 }.createDelegate(this);
\r
1122 f = function(form, callback, scope){
\r
1123 this.doForm(c, m, form, callback, scope);
\r
1124 }.createDelegate(this);
\r
1133 getTransaction: function(opt){
\r
1134 return opt && opt.tid ? Ext.Direct.getTransaction(opt.tid) : null;
\r
1137 doCallback: function(t, e){
\r
1138 var fn = e.status ? 'success' : 'failure';
\r
1141 result = Ext.isDefined(e.result) ? e.result : e.data;
\r
1142 if(Ext.isFunction(hs)){
\r
1145 Ext.callback(hs[fn], hs.scope, [result, e]);
\r
1146 Ext.callback(hs.callback, hs.scope, [result, e]);
\r
1151 Ext.Direct.PROVIDERS['remoting'] = Ext.direct.RemotingProvider;