Skip to content

Instantly share code, notes, and snippets.

@mtetlow
Created June 3, 2013 15:54
Show Gist options
  • Save mtetlow/5699146 to your computer and use it in GitHub Desktop.
Save mtetlow/5699146 to your computer and use it in GitHub Desktop.
This is a first pass at creating a Salesforce.com APEX Remote Action wrapper for jQuery deferred objects. This will allow you to utilize the jQuery.deferred methods (http://api.jquery.com/category/deferred-object/), and avoid lengthy strings of callback methods.
function remoteActionDeferredWrap(remoteActionMethod, paramArr, escape){
//Create the deferred object
var deferredObj=$j.Deferred();
//Create the callback method, this will manipulate the deferred object to show when complete
var callback = function(result,status){
if(status.status==true){
deferredObj.resolve(result);
}
else{
deferredObj.reject('Error: '+status.message);
}
}
//Run the remote action
paramArr.push(callback);
//If we want to override the escape parameter, do so
if(escape!=null){
paramArr.push({"escape":escape});
}
remoteActionMethod.apply(this,paramArr);
return deferredObj.promise();
}
/*Example usage*/
var someMethodPromise=remoteActionDeferredWrap(controller.SomeMethod, ['somevalue',someVar], false);
someMethodPromise.then(
//Success!
function(data){console.log(data);},
//Rejection :(
function(error){console.log(error);}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment