Visualforce: Using JavaScript Remoting for Apex Controllers

  • View
    140

  • Download
    7

Embed Size (px)

Text of Visualforce: Using JavaScript Remoting for Apex Controllers

  • AgendaCovered Topic-JavaScript Remoting for Apex Controllers In Cloud Computing

    Prabhat Gangwar Software Developer

  • What is JavaScript Remoting for Apex Controllers?JavaScript Remoting for Apex Controllers

    AJAX request from an apex page to a controller. Invoke Controller method Synchronous request to the controller with an asynchronous response to the page . A way to separate the page from the controller.

    without the need to perform a form submission. This way to implement light weight visualforce page with faster responce from database instead using action function.

  • What are the benefits?JavaScript Remoting for Apex Controllers

    Decouples the page from the controller. No Need reloading the entire page. avoid View-State issue when you use JS remoting because you will just give request and back the data to the user but not keep it on the server.

  • JavaScript Remoting for Apex Controllers

    What are the disadvantages?Decouples the page from the controller. A basic understanding of javascript is required. Slightly higher barrier to entry.

  • JavaScript Remoting for Apex Controllers

    How to use JavaScript Remoting for Apex Controllers?add the request as a JavaScript invocation with the following form: Syntax [namespace.]controller.method( [parameters...,]callbackFunction, [configuration]); namespaceis the namespace of the controller class.controlleris the name of yourApexcontroller. methodis the name of theApexmethod youre calling.parametersis the comma-separated list of parameters that your method takes.callbackFunctionis the name of the JavaScript function that will handle the response from the controller . You can also declare an anonymous function inline.configurationconfigures the handling of the remote call and response. Use this to change the behavior of a remoting call .

  • JavaScript Remoting for Apex Controllers

    . Example 1 -Before any EventButton will Invoke Controller Method

  • JavaScript Remoting for Apex Controllers

    . After Event

    Result

  • JavaScript Remoting for Apex Controllers

    function getText() { var text = ""; JSRemotingBasicsController.doGetText( function(result, event) { if(event.type === 'exception') { console.log("exception"); console.log(event); } else if (event.status) { alert(result); // here will come welcome to cccinfotech text = result; document.getElementById('{!$Component.page.textResponseApexOutputText}').innerHTML = text; } else { console.log(event.message); } }, { buffer: true, escape: true, timeout: 30000} ); } Simple Javascript Remoting Examples Get Static Text Visual forcepage

  • JavaScript Remoting for Apex Controllers

    Controller -global class JSRemotingBasicsController { public JSRemotingBasicsController() { } @RemoteAction public static String doGetText() { return 'Welcome to cccinfotech'; }} Theglobalaccess modifier declares that this class is known by allApexcode everywhere. When javaScript Remotingmethod is called, It finds@RemoteActionmethods in the controller.

  • JavaScript Remoting for Apex Controllers

    Before any EventExample 2 -

  • JavaScript Remoting for Apex Controllers

    After EventExample 2 -

  • JavaScript Remoting for Apex Controllers

    function getAccountJS() { var accountNameJS = document.getElementById('accName').value; sample.getAccount( accountNameJS,function(result, event) { if (event.status) { // demonstrates how to get ID for HTML and Visualforce tags document.getElementById("{!$Component.theBlock.thePageBlockSection.theFirstItem.accId}").innerHTML = result.Id; document.getElementById("{!$Component.theBlock.thePageBlockSection.theSecondItem.accNam}").innerHTML = result.Name; } else if (event.type === 'exception') { document.getElementById("errors-js").innerHTML = event.message; } else { document.getElementById("errors-js").innerHTML = event.message; } }, ); } Account Name : Get Account Visualforce Page -

  • JavaScript Remoting for Apex Controllers

    Controller -public class sample { public static Account account { get; set; } public sample() { } @RemoteAction public static Account getAccount(String accountName) { account = [select id, name, phone, type, numberofemployees from Account where name = :accountName ]; return account; }}

  • JavaScript Remoting for Apex Controllers

    Configure a remoting request by providing an object.default configuration parameters look like this: { buffer: true, escape: true, timeout: 30000 } Buffer : Boolean Type , The default istrue. This buffering improve the efficiency of the overall request-and-response cycle. Escape :Boolean Type , The default istrue. Apexmethods response. Timeout :-The timeout for the request, in milliseconds. default is30000ms (30seconds). maximum is120000(120seconds, or2minutes). Configuring JavaScript Remoting Requests ?

  • Maxretries : Integer Type retries for the request when connection errors occur. The default is0. The maximum is3. new parameter could be as follows ? { buffer: true, escape: true, timeout: 30000, maxretries: 2 }JavaScript Remoting for Apex Controllers

  • . Important about Remote Action ? JavaScript Remoting for Apex ControllersJavascript remoting calls don't count against SOQL Governor limit.

  • JavaScript Remoting for Apex Controllers

    JavaScript Remoting Campare with Action Function ?Action FuntionAction Function posts the data. Must need of form . View-State issue need to ReRender

    methods are instance methods require bandwidth not less

    @RemoteActioncommunicate with VF page's controller's methods without posting your form.

    avoid View-State issue No need to ReRender methods are static methods require less bandwidth

  • JavaScript Remoting for Apex Controllers

    JavaScript Remoting Campare with Action Function ?Action Funtionhas to transfer the page view state cannot update the page's view state.@RemoteAction

    Less server processing time, because only the data you submit is visible and the view state is not transferred. Controller methods can return data directly back to the calling JavaScript,cannot update the page's view state

  • *JavaScript Remoting for Apex Controllers*JavaScript Remoting for Apex Controllers