Monday, 28 July 2014

visualforce: Advanced Javascript Remoting

 
We have discussed the basic remote javascripting, here I will go with advance visualforce remote javascripting. Adavnced remote javascripting provides you flexibilty and high perfomance of pages.
  
Adding JavaScript Remoting to a Visualforce Page
To use JavaScript remoting in a Visualforce page, add the request as a JavaScript invocation with the following form:
[namespace.]controller.method(
    [parameters...,]
    callbackFunction,
    [configuration]
);

namespace is the namespace of the controller class. This is required if your organization has a namespace defined, or if the class comes from an installed package.      There are two alternatives to define remote javascript without manually adding namespace. Below two examples generate namespace by its own.

 1.  Use  remoteaction global variable.
            {!$RemoteAction.classname.methodname}(
                                [parameters...,]
                                callbackFunction,
                                [configuration]
                );  

2. Use visualforce remoting manager  
        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.AccountRemoter.getAccount}',
            accountName,
            handleResult
        );

 controller is the name of your Apex controller.

 method is the name of the Apex method you’re calling.
   
 parameters is the comma-separated list of parameters that your method takes.callbackFunction is the name of the JavaScript function that will handle the response from the controller. You can also declare an anonymous function inline. callbackFunction receives the status of the method call and the result as parameters.

configuration configures the handling of the remote call and response. Use this to change the behavior of a remoting call, such as whether or not to escape the Apex method’s response.
The remote method call executes synchronously, but it doesn’t wait for the response to return. When the response returns, the callback function handles it asynchronously.  
Here I will explain about  the configuration option  in detail. The default configuration parameters look like this: 

{ buffer: true, escape: true, timeout: 30000 } 

buffer
 Of type Boolean, whether to group requests executed close to each other in time into a single request. The default is true.
JavaScript remoting optimizes requests that are executed close to each other in time and groups the calls into a single request. This buffering improve the efficiency of the overall request-and-response cycle, but sometimes it’s useful to ensure all requests execute independently.
escape
Of type  Boolean, whether to escape the Apex method’s response. The default is true.
timeout  
 Of type integer, the timeout for the request, in milliseconds. The default is 30000 (30 seconds). The maximum is 120000 (120 seconds, or 2 minutes).

The response to a remote method call is handled asynchronously by the callback function provided in the remote method call. Your callback function will receive as parameters an event object representing the status of the remote call, and theresult object returned by the remote Apex method. Your function can update information and user interface elements on the page based on the data returned.
The event object provides values that let you act upon the success or failure of the remote call:
event.status is true on success, false on error.
event.type is the type of the response: rpc for a successful call, exception if the remote method threw an exception, and so on.
event.message contains any error message that is returned.
event.where contains the Apex stack trace, if one was generated by the remote method.
Apex primitive data types returned by result—such as strings or numbers—are converted to their JavaScript equivalents. Apex objects that are returned are converted to JavaScript objects, while collections are converted to a JavaScript array. Keep in mind that JavaScript is case-sensitive, so idId, and ID are considered different fields.
As part of a JavaScript remote call, if the Apex method response contains references to the same object, the object won't be duplicated in the returned JavaScript object, and instead, the rendered JavaScript object will contain references to the same object. An example is an Apex method which returns a list that contains the same object twice.
By default, the response of the remote call must return within 30 seconds, after which the call will time out. If your request needs longer to complete, configure a longer timeout, up to 120 seconds.
The response of the remote call has a maximum size of 15 MB.
If your JavaScript remoting code is exceeding this limit, you have several options:
Reduce the size of the response for each request. Only return data that’s required.
Break up large data retrieval into requests that return smaller chunks.
Ensure that you’re using non-batched requests. Set { buffer: false } in your remoting request configuration block.
Make batched requests less frequently, reducing the size of the batch.

Javascript Remoting and Action Fuction

<apex:actionFunction> component also lets you call controller action methods through JavaScript. Here are some differences between the two:
The <apex:actionFunction> tag:
lets you specify rerender targets
submits the form
does not require you to write any JavaScript
JavaScript remoting:
lets you pass parameters
provides a callback
requires you to write some JavaScript
In general, <apex:actionFunction> is easier to use and requires less code, while JavaScript remoting offers more flexibility.




No comments:

Post a Comment