HTML/Web Storage: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
Line 57: Line 57:
         getData  : function(name, secured, store, data){
         getData  : function(name, secured, store, data){
             if(name && (store = secured ? session : local)){
             if(name && (store = secured ? session : local)){
                 return JSON.parse(store.getItem(name));
                 data = JSON.parse(store.getItem(name));
                me[name] = data;
                return data;
             }
             }
         },
         },
Line 63: Line 65:
             if(name && data && (store = secured ? session : local)){
             if(name && data && (store = secured ? session : local)){
                 store.setItem(name, JSON.stringify(data));
                 store.setItem(name, JSON.stringify(data));
                me[name] = data;
                 return true;
                 return true;
             }
             }

Revision as of 07:18, 27 September 2018

Prototyping Library

//prototyping for string format
;String.prototype.format||(function(){
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function(match, number) {
            return typeof args[number] != 'undefined' ? args[number] : match;
        });
    };
})();

//prototyping for console info
;String.prototype.info||(function(){
    String.prototype.info = function() {
        if(console && console.info){
            console.info(this.toString());
        }
        return this;
    };
})();

Proprietary Library

//chorke main namespace/package
var Chorke=Chorke||(function(me){
    me = {
        $class   : 'Chorke',
        $package : 'Default',
        meta     : function(ns){
            var tpl = '{\n  "package" : "{0}",\n  "class"   : "{1}"\n}',
                out = tpl.format(ns.$package, ns.$class);
            return out;
        }
    };
    return me;
})(),
//chorke alias name
CKi=CKi||(function(me){
    me.$class = 'CKi';
    me.$meta  = function(){return me.meta(me)};
    return me;
})(Chorke);

Database Engine

;CKi.db||(function($, win, me){
    var session = win && win.sessionStorage,
        local   = win && win.localStorage;
        
    me  = {
        $package : 'CKi',
        $class   : 'CKi.db',
        $meta    : function(){return $.meta(me)},
        getData  : function(name, secured, store, data){
            if(name && (store = secured ? session : local)){
                data = JSON.parse(store.getItem(name));
                me[name] = data;
                return data;
            }
        },
        setData  : function(name, data, secured, store){
            if(name && data && (store = secured ? session : local)){
                store.setItem(name, JSON.stringify(data));
                me[name] = data;
                return true;
            }
        }
    };
    $.db = me;
})(CKi, window);

Checking Output

//store data into local/session storage
CKi.db.setData('T00I00', {id: 2, name: 'Selina'}, true);
CKi.db.setData('T00I00', {id: 1, name: 'Raiyan'});

//get data from local/session storage
CKi.db.getData('T00I00', true);
CKi.db.getData('T00I00');

References