The obvious way to try this is to set the fields
list to a Visualforce expression. Unfortunately, although that does compile, it doesn't work:
<apex:remoteObjects jsNamespace="Test">
<apex:remoteObjectModel name="Account" fields="{! fieldList }">
</apex:remoteObjectModel>
</apex:remoteObjects>
You get back the Id
only when you perform a retrieve.
However, you can do this with a remote objects action override.
Visualforce Page
<apex:page controller="TestRemoteActionOverrideController">
<apex:remoteObjects jsNamespace="Test">
<apex:remoteObjectModel name="Account" fields="Id"
retrieve="{! $RemoteAction.TestRemoteActionOverrideController.retrieve }" />
</apex:remoteObjects>
<script>
var act = new Test.Account();
act.retrieve(
{ },
function(error, results, event) {
alert('Got results: ' + JSON.stringify(results) + ' Error was: ' + error);
}
);
</script>
</apex:page>
Apex Controller
public class TestRemoteActionOverrideController{
@RemoteAction
public static Map<String,Object> retrieve(String type, List<String> fields, Map<String, Object> criteria) {
fields.add('Name');
// call through to the default remote object controller with our modified field list.
return RemoteObjectController.retrieve(type, fields, criteria);
}
}
If you load this page, you'll find both the Name
and Id
fields populated in the alert message. You can, if necessary, override other remote actions accordingly.