Snippets

Johannes Heinen Visualforce Remoting Action Wrapper

Created by Johannes Heinen
(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)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.