Snippets
Created by
Johannes Heinen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | (function(angular, vf) {
/**
* Constructor
*/
function RemotingAction(config) {
// CONFIG OBJECT
this._config = angular.extend({
buffer : true,
escape : true,
timeout : 30000
}, config);
// HANDLERS
this._handlers = {
onError : [],
onException : [],
onComplete : [],
onSuccess : []
};
}
/**
* Returns an array with all available handlers of
* the fiven type. Currently available handlers:
*
* - onError
* - onException
* - onComplete
* - onSuccess
*
* To add a new handler, simply use Array.push on one
* of the object properties. Or set the property directly --
* handler engine does it gracefully
*
* @return object
*/
RemotingAction.prototype.getHandler = function(name) {
var handlers = this._handlers[name];
if(typeof handlers === 'undefined') {
throw new 'No handler named "' + name + '" found';
}
if( ! angular.isArray(handlers)) {
handlers = [handlers];
}
return handlers;
}
/**
* Binds a handler to the given type. Currently available
* types are:
*
* - onError
* - onException
* - onComplete
* - onSuccess
*
* @return object
*/
RemotingAction.prototype.addHandler = function(name, handler) {
if( ! angular.isArray(this._handlers[name])) {
this._handlers[name] = [this._handlers[name]];
}
this._handlers[name].push(handler);
}
RemotingAction.prototype.doRequest = function(url, params) {
if( ! angular.isString(url) || url.length === 0) {
throw 'No Url String provided';
}
params = angular.extend({
attributes: undefined,
onSuccess : undefined,
onError: undefined,
onException: undefined,
onComplete: undefined
}, params);
if(typeof params.config === 'undefined') {
params.config = this._config;
} else {
params.config = angular.extend(this._config, params.config);
}
var req = [url];
if(typeof params.attributes !== 'undefined') {
if(angular.isArray(params.attributes)) {
req = req.concat(params.attributes);
} else {
req.push(params.attributes);
}
}
var self = this;
// ON-COMPLETE CALLBACK
req.push(function(response, event) {
// SUCCESS
if (event.status) {
// EVAL SUCCESS HANDLERS
self._applyHandlers('onSuccess', params.onSuccess, arguments);
// ERROR
} else {
if(event.type === 'exception') {
self._applyHandlers('onException', params.onException, arguments);
} else {
self._applyHandlers('onError', params.onError, arguments);
}
}
self._applyHandlers('onComplete', params.onComplete, arguments);
});
req.push(params.config);
Visualforce.remoting.Manager.invokeAction.apply(Visualforce.remoting.Manager, req);
}
RemotingAction.prototype._applyHandlers = function(globalHandlerName, localHandler, args) {
var handlers = this.getHandler(globalHandlerName).slice(0); // CLONE
if(angular.isFunction(localHandler)) {
handlers.push(localHandler);
}
for(var i = 0, len = handlers.length; i < len; i++) {
console.info('apply handler "' + globalHandlerName + '"');
handlers[i].apply(this, args);
}
}
RemotingAction.prototype.invoke = function(url, attributes, onSuccess, params)
{
if( ! angular.isString(url) || url.length === 0) {
throw 'No Url String provided';
}
if(angular.isDefined(arguments[1])) {
if(angular.isFunction(arguments[1])) {
onSuccess = arguments[1];
}
if(angular.isDefined(arguments[2])) {
if( ! angular.isFunction(arguments[2])) {
params = arguments[2];
}
}
}
var toInject = {
onSuccess : onSuccess,
attributes : attributes
};
if(typeof params === 'undefined') {
params = toInject;
} else {
params = angular.extend(params, toInject);
}
return this.doRequest(url, params);
}
// BIND TO NS
vf.RemotingAction = RemotingAction;
})(angular, vf);
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.