HTML/Web Storage: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
==Prototyping==
==Prototyping Library==
<syntaxhighlight lang="js">
<syntaxhighlight lang="js">
//prototyping for string format
;String.prototype.format||(function(){
;String.prototype.format||(function(){
     String.prototype.format = function() {
     String.prototype.format = function() {
Line 9: Line 10:
     };
     };
})();
})();
//prototyping for console info
;String.prototype.info||(function(){
;String.prototype.info||(function(){
     String.prototype.info = function() {
     String.prototype.info = function() {
Line 19: Line 22:
</syntaxhighlight>
</syntaxhighlight>


==Proprietary==
==Proprietary Library==
<syntaxhighlight lang="js">
<syntaxhighlight lang="js">
//chorke main namespace/package
var Chorke=Chorke||(function(me){
var Chorke=Chorke||(function(me){
     me = {
     me = {
Line 33: Line 37:
     return me;
     return me;
})(),
})(),
//chorke alias name
CKi=CKi||(function(me){
CKi=CKi||(function(me){
     me.$class = 'CKi';
     me.$class = 'CKi';

Revision as of 07:11, 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)){
                return JSON.parse(store.getItem(name));
            }
        },
        setData  : function(name, data, secured, store){
            if(name && data && (store = secured ? session : local)){
                store.setItem(name, JSON.stringify(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