Created
June 3, 2013 15:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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