HTML/Web Storage: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 40: | Line 40: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Database== | ==Database Engine== | ||
<syntaxhighlight lang="js"> | <syntaxhighlight lang="js"> | ||
;CKi.db||(function($, win, me){ | ;CKi.db||(function($, win, me){ |
Revision as of 07:05, 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 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');