JavaScript Remoting
One of the major enhancements from visualforce this release(Summer 11) and my favorite is JavaScript remoting.
JavaScript Remoting is the ability to invoke apex class method from javascript embedded in a visualforce page.JavaScript Remoting is now available after summer 11 release.
Here is a quick example : we have a Apex class which defines a get Account method.
global with sharing class MyJSRemoting { public static Account account { get; set; } @RemoteAction global static Account getAccount(String accountName) { account = [select id, name, phone, type, numberofemployees from Account where name = :accountName limit 1]; return account; } }
This method is having @RemoteAction annotation making it available to be called from a visualforce page. Now in our visualforce page we have a script tag set which is how we embed javascript within visualforce pages.
<apex:page controller="MyJSRemoting"> <script type="text/javascript"> function getAccountJS() { var accountNameJS = document.getElementById('accountname').value; ankit.MyJSRemoting.getAccount( accountNameJS, function(result, event) { if (event.status) { document.getElementById("{!$Component.theBlock.pbs.pbsi2.accId}") .innerHTML = result.Id; document.getElementById("{!$Component.theBlock.pbs.pbsi1.name}") .innerHTML = result.Name; } }, {escape:true}); } </script> <input id="accountname" type="text"> <button onclick="getAccountJS();">Get Account</button> <apex:pageblock id="theBlock"> <apex:pageblocksection columns="2" id="pbs"> <apex:pageblocksectionitem id="pbsi1"> <apex:outputtext id="name"> </apex:outputtext></apex:pageblocksectionitem> <apex:pageblocksectionitem id="pbsi2"> <apex:outputtext id="accId"> </apex:outputtext></apex:pageblocksectionitem> </apex:pageblocksection> </apex:pageblock> </apex:page>
This JavaScript code then invokes the getAccount method in the apex class to take action.
Please note I have used namespace “ankit.MyJSRemoting” as there is a namespace registered on my organization.
JavaScript Remoting is the feature which is primarily be used by developers and will allow them to create richer and more interactive user interfaces that everyone will benefit from.
In particular, lets developers create rich javascript based user interfaces by invoking apex controller methods within JavaScript code. JavaScript is typically used to render small parts of user interface. Result from a Javascript remoting call are much faster then using a typical visualforce reRender model, developers can provide more instant feedback to the user. This is because remoting calls are stateless. Means only data is set back and forth instead a lot of bulky HTML.
Additional Visualforce enhancements :
1) Filtered Lookup via Visualforce.
2) Inline editing for rich text area field.
3) Field Set property accessors.
2) Inline editing for rich text area field.
3) Field Set property accessors.