HTML5, CSS3 & JS Experiments

By Martin Ivanov

Mapper for RESTFul Methods

Register the Class

<script src="AcidJs.RestMethodMapper/jquery-1.8.2.min.js"></script>
<script src="AcidJs.RestMethodMapper/AcidJs.RestMethodMapper.js"></script>

Usage and Code Insight

(function() {
    window.mapper = new AcidJs.RestMethodMapper("rest/", {
        "user/get/": {
            type: "GET",
            cache: false,
            success: function(response) {
                console.log("success:", response);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log("error: ", jqXHR, textStatus, errorThrown);
            },
            dataType: "json"
        },
        "user/get/my/": {
            type: "get",
            success: function() {

            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log("error: ", jqXHR, textStatus, errorThrown);
            },
            dataType: "json"
        },
        "msg/post/new/": {
            type: "post",
            success: function() {

            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log("error: ", jqXHR, textStatus, errorThrown);
            },
            dataType: "json"
        }
    });

    window.mapper.userGet({
        firstName: "John",
        lastName: "Smith",
        id: 9999
    });
    window.mapper.userGetMy();
    window.mapper.msgPostNew({
        id: 1234,
        body: "Lorem ipsum dolot sit amet"
    });
})();
(function() {
    "use strict";
    
    /*
     * @namespace AcidJs
     **/
    if(undefined === window.AcidJs) {
        window.AcidJs = {};
    }
    
    /*
     * @class RestMethodMapper
     * @constructor
     * @param endpoint {String}
     * @param method {String} rest api method, for example "user/get/my"
     * @author Martin Ivanov http://acidjs.wemakesites.net
     **/
    function RestMethodMapper(endpoint, method) {
        endpoint = endpoint || "";
        method = method || {};
        
        var
            that = this;
            
        $.each(method, function(key, value) {
            (function() {
                var 
                    name = that.restToMethod(key);
                
                if(!that[name]) {
                    var
                        ajaxConfig = {
                            url: endpoint + key
                        };
                    
                    if(value) {
                        $.each(value, function(k, v) {
                            ajaxConfig[k] = v;
                        });
                    }
                    
                    that[name] = function(data) {
                        if(data) {
                            ajaxConfig.data = data;
                        }
                        $.ajax(ajaxConfig);
                    };
                }
            })();
        });
    }
    
    RestMethodMapper.prototype = {
        /*
         * @method restToMethod convert RESTFul api method to a JavaScript method
         * @param url {String}
         * @return {String} 
         **/
        restToMethod: function(url){
            if(!url) {
                return "";
            }
            
            var 
                path = url.split("/"),
                methodName = "";
                
            for(var i = 0, len = path.length; i < len; i++) {
                methodName += (i === 0) ? path[i] : path[i].charAt(0).toUpperCase() + path[i].substr(1);
            }
            
            return methodName;
        }
    };
    
    window.AcidJs.RestMethodMapper = RestMethodMapper;
})();

Please, disable your ad blocker

This website is made possible by displaying online advertisements to the visitors. To view the content, please, disable your ad blocker, then refresh the page.