/** * @class Ext.direct.RemotingProvider * *
The {@link Ext.direct.RemotingProvider RemotingProvider} exposes access to * server side methods on the client (a remote procedure call (RPC) type of * connection where the client can initiate a procedure on the server).
* *This allows for code to be organized in a fashion that is maintainable, * while providing a clear path between client and server, something that is * not always apparent when using URLs.
* *To accomplish this the server-side needs to describe what classes and methods * are available on the client-side. This configuration will typically be * outputted by the server-side Ext.Direct stack when the API description is built.
*/ Ext.define('Ext.direct.RemotingProvider', { /* Begin Definitions */ alias: 'direct.remotingprovider', extend: 'Ext.direct.JsonProvider', requires: [ 'Ext.util.MixedCollection', 'Ext.util.DelayedTask', 'Ext.direct.Transaction', 'Ext.direct.RemotingMethod' ], /* End Definitions */ /** * @cfg {Object} actions * Object literal defining the server side actions and methods. For example, if * the Provider is configured with: *
"actions":{ // each property within the 'actions' object represents a server side Class
"TestAction":[ // array of methods within each server side Class to be
{ // stubbed out on client
"name":"doEcho",
"len":1
},{
"name":"multiply",// name of method
"len":2 // The number of parameters that will be used to create an
// array of data to send to the server side function.
// Ensure the server sends back a Number, not a String.
},{
"name":"doForm",
"formHandler":true, // direct the client to use specialized form handling method
"len":1
}]
}
*
* Note that a Store is not required, a server method can be called at any time. * In the following example a client side handler is used to call the * server side method "multiply" in the server-side "TestAction" Class:
*
TestAction.multiply(
2, 4, // pass two arguments to server, so specify len=2
// callback function after the server is called
// result: the result returned by the server
// e: Ext.direct.RemotingEvent object
function(result, e){
var t = e.getTransaction();
var action = t.action; // server side Class called
var method = t.method; // server side method called
if(e.status){
var answer = Ext.encode(result); // 8
}else{
var msg = e.message; // failure message
}
}
);
*
* In the example above, the server side "multiply" function will be passed two
* arguments (2 and 4). The "multiply" method should return the value 8 which will be
* available as the result in the example above.
*/
/**
* @cfg {String/Object} namespace
* Namespace for the Remoting Provider (defaults to the browser global scope of window).
* Explicitly specify the namespace Object, or specify a String to have a
* {@link Ext#namespace namespace created} implicitly.
*/
/**
* @cfg {String} url
* Required. The url to connect to the {@link Ext.direct.Manager} server-side router.
*/
/**
* @cfg {String} enableUrlEncode
* Specify which param will hold the arguments for the method.
* Defaults to 'data'.
*/
/**
* @cfg {Number/Boolean} enableBuffer
* true or false to enable or disable combining of method * calls. If a number is specified this is the amount of time in milliseconds * to wait before sending a batched request.
*Calls which are received within the specified timeframe will be * concatenated together and sent in a single request, optimizing the * application by reducing the amount of round trips that have to be made * to the server.
*/ enableBuffer: 10, /** * @cfg {Number} maxRetries * Number of times to re-attempt delivery on failure of a call. */ maxRetries: 1, /** * @cfg {Number} timeout * The timeout to use for each request. */ timeout: undefined, constructor : function(config){ var me = this; me.callParent(arguments); me.addEvents( /** * @event beforecall * Fires immediately before the client-side sends off the RPC call. * By returning false from an event handler you can prevent the call from * executing. * @param {Ext.direct.RemotingProvider} provider * @param {Ext.direct.Transaction} transaction * @param {Object} meta The meta data */ 'beforecall', /** * @event call * Fires immediately after the request to the server-side is sent. This does * NOT fire after the response has come back from the call. * @param {Ext.direct.RemotingProvider} provider * @param {Ext.direct.Transaction} transaction * @param {Object} meta The meta data */ 'call' ); me.namespace = (Ext.isString(me.namespace)) ? Ext.ns(me.namespace) : me.namespace || window; me.transactions = new Ext.util.MixedCollection(); me.callBuffer = []; }, /** * Initialize the API * @private */ initAPI : function(){ var actions = this.actions, namespace = this.namespace, action, cls, methods, i, len, method; for (action in actions) { if (actions.hasOwnProperty(action)) { cls = namespace[action]; if (!cls) { cls = namespace[action] = {}; } methods = actions[action]; for (i = 0, len = methods.length; i < len; ++i) { method = new Ext.direct.RemotingMethod(methods[i]); cls[method.name] = this.createHandler(action, method); } } } }, /** * Create a handler function for a direct call. * @private * @param {String} action The action the call is for * @param {Object} method The details of the method * @return {Function} A JS function that will kick off the call */ createHandler : function(action, method){ var me = this, handler; if (!method.formHandler) { handler = function(){ me.configureRequest(action, method, Array.prototype.slice.call(arguments, 0)); }; } else { handler = function(form, callback, scope){ me.configureFormRequest(action, method, form, callback, scope); }; } handler.directCfg = { action: action, method: method }; return handler; }, // inherit docs isConnected: function(){ return !!this.connected; }, // inherit docs connect: function(){ var me = this; if (me.url) { me.initAPI(); me.connected = true; me.fireEvent('connect', me); } else if(!me.url) { //