// @tag enterprise /** * @class Ext.direct.AmfRemotingProvider * *
The {@link Ext.direct.AmfRemotingProvider AmfRemotingProvider} * allows making RPC calls to a Java object on a BlazeDS or ColdFusion using either the AMFX or the AMF protocols.
* *The default protocol is AMFX which works on all browsers. If you choose AMF, a flash plugin might be loaded in certain browsers that do not support posting binary data to the server, e.g. Internet Explorer version 9 or less. To choose AMF, set the {@link Ext.direct.AmfRemotingProvider#binary binary} property to true.
*For AMFX, the server must be configured to expose the desired services via an HTTPEndpoint. For example, the following configuration snippet adds an HTTPEndpoint (AMFX endpoint) to the BlazeDS services-config.xml file:
*
<channel-definition id="my-http" class="mx.messaging.channels.HTTPChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/http" class="flex.messaging.endpoints.HTTPEndpoint"/>
</channel-definition>
*
* Once the HTTPEndpoint is configured, make sure the service is exposed via the channel by adding the channel (e.g. my-http) to your remoting-services.xml file. * For example this allows services to be accessed remotely by both AMF and AMFX:
*
<default-channels>
<channel ref="my-amf"/>
<channel ref="my-http"/>
</default-channels>
*
*
* In order to make a call, you first need to declare the API to Ext direct. The following example defines local methods to the services provided by the sample Products application provided by Adobe as part of the BlazeDS 4.x binary turnkey distribution's testdrive (Sample 5: Updating Data):
*
Ext.direct.Manager.addProvider({
"url":"/samples/messagebroker/http", // URL for the HTTPEndpoint
"type":"amfremoting",
"endpoint": "my-http", // the name of the HTTPEndpoint channel as defined in the server's services-config.xml
"actions":{
"product":[{ // name of the destination as defined in remoting-config.xml on the server
"name":"getProducts", // method name of the method to call
"len":0 // number of parameters
},{
"name":"add",
"len":1
},{
"name":"bad",
"len":0
}]
}
});
*
* You can now call the service as follows:
product.getProducts((function(provider, response) {
// do something with the response
console.log("Got " + response.data.length + " objects");
});
*
* Note that in case server methods require parameters of a specific class (e.g. flex.samples.product.Product), you should make sure the passed parameter has a field called $flexType set to the class name (in this case flex.Samples.product.Product). This is similar to the remote class alias definition in ActionScript.
*
*
* The following example shows how to define a binary AMF-based call:
*
Ext.direct.Manager.addProvider({
"url":"/samples/messagebroker/amf", // URL for the AMFEndpoint
"type":"amfremoting",
"endpoint": "my-amf", // the name of the AMFEndpoint channel as defined in the server's services-config.xml
"binary": true, // chooses AMF encoding
"actions":{
"product":[{ // name of the destination as defined in remoting-config.xml on the server
"name":"getProducts", // method name of the method to call
"len":0 // number of parameters
},{
"name":"add",
"len":1
},{
"name":"bad",
"len":0
}]
}
});
*
* Calling the server is done the same way as for the AMFX-based definition.
*/ Ext.define('Ext.direct.AmfRemotingProvider', { /* Begin Definitions */ alias: 'direct.amfremotingprovider', extend: 'Ext.direct.Provider', requires: [ 'Ext.util.MixedCollection', 'Ext.util.DelayedTask', 'Ext.direct.Transaction', 'Ext.direct.RemotingMethod', 'Ext.data.amf.XmlEncoder', 'Ext.data.amf.XmlDecoder', 'Ext.data.amf.Encoder', 'Ext.data.amf.Packet', 'Ext.data.amf.RemotingMessage', 'Ext.direct.ExceptionEvent' ], /* 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 Flex remoting server (LCDS, BlazeDS, etc).
* This should include the /messagebroker/amf suffix as defined in the services-config.xml and remoting-config.xml files.
*/
/**
* @cfg {String} endpoint
* Requred. This is the channel id defined in services-config.xml on the server (e.g. my-amf or my-http).
*/
/**
* @cfg {String} enableUrlEncode
* Specify which param will hold the arguments for the method.
* Defaults to 'data'.
*/
/**
* @cfg {String} binary
* If true, use AMF binary encoding instead of AMFX XML-based encoding. Note that on some browsers, this will load a flash plugin to handle binary communication with the server. Important: If using binary encoding with older browsers, see notes in {@link Ext.data.flash.BinaryXhr BinaryXhr} regarding packaging the Flash plugin for use in older browsers.
*/
binary: false,
/**
* @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,
/**
* @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.AmfRemotingProvider} provider
* @param {Ext.direct.Transaction} transaction
* @param {Object} meta The meta data
*/
/**
* @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.AmfRemotingProvider} provider
* @param {Ext.direct.Transaction} transaction
* @param {Object} meta The meta data
*/
constructor : function(config){
var me = this;
me.callParent(arguments);
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) {
// Generate a unique ID for this client
me.clientId = Ext.data.amf.XmlEncoder.generateFlexUID();
me.initAPI();
me.connected = true;
me.fireEvent('connect', me);
me.DSId = null;
} else if(!me.url) {
//