One of the exciting feature of Spring14 release is introduction of “Visualforce Remote Objects”. You can say its actually replacement of JavaScript Remoting.
Why do we need “Visualforce Remote Objects” when we already have “JavaScript Remoting” ?
Well, here are few advantages of “Visualforce Remote Objects” :
- No need to write Controllers, Everything can be done in Visualforce only.
- As @RemoteAction annotated methods needs to be static so you had to take special precaution as it didn’t supported Viewstate. This hurdle is completely removed now.
- No need to write Test Class now, as no Controller is involved.
How to start with this ?
At time of writing this article, This feature is under Pilot release. So, you have to contact Salesforce support to enable it in your Organization.
Visualforce code Sample :
<!-- This Demo will assume Querying Account Object --> <apex:remoteObjects> <apex:remoteObjectModel name="Account" jsShorthand="getActs" fields="Name,Id"> <apex:remoteObjectField name="ClientType__c" jsShorthand="cType"> </apex:remoteObjectModel> </apex:remoteObjects>
you can see in above code, few new Visualforce tags are introduced like “remoteObjectModel” and “remoteObjectField“.
jsShorthand attribute defines shortcut that can be used inside your JavaScript code. Now, we don’t have to write annoying object or field name ending with “__c” or namespace name. This will keep our code tidy.
Javascript code Sample :
//Create new Remote Object Reference var src = new SObjectModel.getActs(); //Use Remote Object to query 5 records src.retrieve({ limit : 10, where : { cType :{ eq : 'Banking' } } } , function(err,records){ if(err == null) { //Process returned "records" to display in Visualforce code. } } );
In above code, we are calling retrieve() method of Javascript object SObjectModel. Once you get records, you can process it in your javascript. Other than retrieve() , you can perform all CRUD operations on object.
You can see below articles also on same topic.
Leave a Reply