Sha256: b9713c73be3d0d88623f4b581be51240e85bf87465b457287ad98a552ef1e3e5

Contents?: true

Size: 1.15 KB

Versions: 9

Compression:

Stored size: 1.15 KB

Contents

// Simple storage plugin
// Written by Daniel Mendler
(function($) {
    "use strict";

    var storage = {}, data = {};
    try {
	if (window.localStorage) {
            storage = window.localStorage;
	}
	if (storage.oleloStorage) {
	    data = JSON.parse(storage.oleloStorage);
	}
    } catch (e) {
        // Firefox fails when touching localStorage/globalStorage and cookies are disabled
    }

    function checkKey(key) {
	if (typeof(key) != 'string' && typeof(key) != 'number') {
	    throw new TypeError('Key name must be string or numeric');
	}
    }

    function save() {
	try {
	    storage.oleloStorage = JSON.stringify(data);
	} catch (e) {
            // probably cache is full, nothing is saved this way
        }
    }

    $.storage = {
	set: function(key, value){
	    checkKey(key);
	    data[key] = value;
	    save();
	    return value;
	},
	get: function(key, def){
	    checkKey(key);
	    if (key in data) {
		return data[key];
	    }
	    return typeof(def) == 'undefined' ? null : def;
	},
	remove: function(key){
	    checkKey(key);
	    if (key in data){
		delete data[key];
		save();
		return true;
	    }
	    return false;
	}
    };
})(jQuery);

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
olelo-0.9.15 static/script/09-olelo.storage.js
olelo-0.9.14 static/script/09-olelo.storage.js
olelo-0.9.13 static/script/09-olelo.storage.js
olelo-0.9.12 static/script/09-olelo.storage.js
olelo-0.9.11 static/script/09-olelo.storage.js
olelo-0.9.10 static/script/09-olelo.storage.js
olelo-0.9.9 static/script/09-olelo.storage.js
olelo-0.9.8 static/script/09-olelo.storage.js
olelo-0.9.7 static/script/09-olelo.storage.js