HTML/Web Storage: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
(Created page with "==References== * [https://hacks.mozilla.org/2010/01/offline-web-applications/ Offline Web Applications] * [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-sid...")
 
No edit summary
Line 1: Line 1:
==Prototyping==
<syntaxhighlight lang="js">
;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;
        });
    };
})();
;String.prototype.info||(function(){
    String.prototype.info = function() {
        if(console && console.info){
            console.info(this.toString());
        }
        return this;
    };
})();
</syntaxhighlight>
==Proprietary==
<syntaxhighlight lang="js">
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;
})(),
CKi=CKi||(function(me){
    me.$class = 'CKi';
    me.$meta  = function(){return me.meta(me)};
    return me;
})(Chorke);
</syntaxhighlight>
==Database==
<syntaxhighlight lang="js">
;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);
</syntaxhighlight>
==Checking Output==
<syntaxhighlight lang="js">
//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');
</syntaxhighlight>
==References==
==References==
* [https://hacks.mozilla.org/2010/01/offline-web-applications/ Offline Web Applications]
* [https://hacks.mozilla.org/2010/01/offline-web-applications/ Offline Web Applications]
* [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Client-side_storage Client Side Storage?]
* [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Client-side_storage Client Side Storage?]
* [https://www.w3schools.com/html/html5_webstorage.asp HTML Web Storage]
* [https://www.w3schools.com/html/html5_webstorage.asp HTML Web Storage]

Revision as of 07:03, 27 September 2018

Prototyping

;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;
        });
    };
})();
;String.prototype.info||(function(){
    String.prototype.info = function() {
        if(console && console.info){
            console.info(this.toString());
        }
        return this;
    };
})();

Proprietary

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;
})(),
CKi=CKi||(function(me){
    me.$class = 'CKi';
    me.$meta  = function(){return me.meta(me)};
    return me;
})(Chorke);

Database

;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