Augment Visualforce Remote Objects to provide promises.
First, jQuery is required, as that's what provides the deferred/promises framework. After that's included, make sure makeDeferredProvider.js
(or makePreferredProvider.min.js
) is included.
Then, simply call
makeDeferredProvider("jsNameSpace");
where jsNameSpace
is the namespace provided to the apex:remoteObjects
VF tag. If no argument is provided for makeDeferredProvider
, the default is SObjectModel
, just like the remoteObjects tag.
The regular flow for Visualforce Remote Object is (per the release notes):
var wh = new SObjectModel.Warehouse();
// Use the Remote Object to query for 10 warehouse records
wh.retrieve({ limit: 10 }, function(err, records){
if(err) alert(err.message);
else {
alert (records);
}
});
Using promises, the flow is just a bit different:
var wh = SObjectModel.deferredObject('Warehouse');;
// Use the Remote Object to query for 10 warehouse records
var whp = wh.retrieve({ limit: 10 });
whp.then(
// The first function is invoked when the promise is successfully fulfilled
function(records){
console.log(records);
},
// The second function is invoked when the promise is rejected
function(err) {
console.log(err);
}
});
That's a bit more verbose, and in this simple case, promises aren't worth the effort. Read up more here, or here to really start to get the advantages promises offer.