Created
December 1, 2014 21:12
-
-
Save marpstar/39fdb53465b59d4440f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
$get: function($q, $rootScope) { | |
function send(remoteAction, options, nullok) { | |
var namespace, controller, method; | |
var Manager = Visualforce.remoting.Manager; | |
var parts = remoteAction.split('.'); | |
var instance = this; | |
if (options && typeof options !== 'object') { | |
throw new Error('Options must be an object'); | |
} | |
if (parts.length < 2) { | |
throw new Error('Invalid Remote Action specified. Use Controller.MethodName or $RemoteAction.Controller.MethodName'); | |
} else { | |
if (parts.length === 3) { | |
namespace = parts[0]; | |
controller = parts[1]; | |
method = parts[2]; | |
} else if (parts.length === 2) { | |
controller = parts[0]; | |
method = parts[1]; | |
} | |
} | |
return function() { | |
var deferred = $q.defer(); | |
var args; | |
if (arguments.length) { | |
args = Array.prototype.slice.apply(arguments); | |
} else { | |
args = []; | |
} | |
args.splice(0, 0, remoteAction); | |
args.push(function(result, event) { | |
instance.handleResultWithPromise(result, event, nullok, deferred); | |
}); | |
if (options) { | |
args.push(options); | |
} | |
Manager.invokeAction.apply(Manager, args); | |
return deferred.promise; | |
}; | |
}; | |
return { | |
standardOptions: standardOpts, | |
/* | |
* Kevin o'Hara released premote, a nice lib for wrapping | |
* visualforce remoting calls in a promise interface. this | |
* function .send() is largely a gentle refactoring of his | |
* work, found in "premote" here: | |
* https://github.com/kevinohara80/premote | |
* such that it locks into the ng exec loop and utilizes | |
* the angular $q service, itself based on the Q lib | |
* Kevin uses. | |
*/ | |
/** | |
* Returns a function that, when called, invokes the js | |
* remoting method specified in this call. | |
* @param {String} remoteAction class.methodName string representing the Apex className and Method to invoke | |
* @param {Object} options Ojbect containing at least the timeout and escaping options. Passed to Remoting call | |
* @param {Boolean} nullok Can this method return null and it be OK? | |
* @return {Function} Function engaged with the NG execution loop, making Visualforce remoting calls. | |
*/ | |
send: send, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment