Mercurial > bugzilla-dashboard
changeset 93:00d23d6d41b4
made cache module into cache/html5 and a factory function.
author | Atul Varma <avarma@mozilla.com> |
---|---|
date | Mon, 26 Apr 2010 19:02:23 -0700 |
parents | 16e98d4313e6 |
children | 5ea520acc44e |
files | js/modules/app.js js/modules/cache.js |
diffstat | 2 files changed, 33 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/js/modules/app.js Mon Apr 26 18:48:58 2010 -0700 +++ b/js/modules/app.js Mon Apr 26 19:02:23 2010 -0700 @@ -1,9 +1,14 @@ Require.modules["app/loader"] = function(exports, require) { exports.init = function init(moduleExports, options) { + var cache = require("cache/html5").create( + "bugzilla-dashboard-cache", + options.window.sessionStorage + ); var bugzilla = require("app/bugzilla-auth").create(options.Bugzilla); + moduleExports.bugzilla = bugzilla; + moduleExports.cache = cache; moduleExports.window = options.window; - moduleExports.storage = options.window.sessionStorage; moduleExports.jQuery = options.jQuery; require("app/ui").init(options.window.document);
--- a/js/modules/cache.js Mon Apr 26 18:48:58 2010 -0700 +++ b/js/modules/cache.js Mon Apr 26 19:02:23 2010 -0700 @@ -1,33 +1,34 @@ -Require.modules["cache"] = function(exports, require) { - const CACHE_NAME = "cache"; - - var storage = require("storage"); - var cache = storage.getItem(CACHE_NAME); +Require.modules["cache/html5"] = function(exports, require) { + exports.create = function create(name, storage) { + var cache = storage.getItem(name); - if (cache) - cache = JSON.parse(cache); - else - cache = {}; + if (cache) + cache = JSON.parse(cache); + else + cache = {}; - exports.set = function set(key, value) { - cache[key] = value; + return { + set: function set(key, value) { + cache[key] = value; - // Remove the key first, to get around a strange iPad - // issue: http://stackoverflow.com/questions/2603682/is-anyone-else-receiving-a-quota-exceeded-err-on-their-ipad-when-accessing-locals - storage.removeItem(CACHE_NAME); + // Remove the key first, to get around a strange iPad + // issue: http://stackoverflow.com/questions/2603682/is-anyone-else-receiving-a-quota-exceeded-err-on-their-ipad-when-accessing-locals + storage.removeItem(name); + + // TODO: We should really catch QUOTA_EXCEEDED_ERR here, + // which could be thrown if the user is in private + // browsing mode. + storage.setItem(name, JSON.stringify(cache)); + }, - // TODO: We should really catch QUOTA_EXCEEDED_ERR here, - // which could be thrown if the user is in private - // browsing mode. - storage.setItem(CACHE_NAME, JSON.stringify(cache)); - }; + get: function get(key) { + return cache[key]; + }, - exports.get = function get(key) { - return cache[key]; - }; - - exports.clear = function clear() { - storage.removeItem(CACHE_NAME); - cache = {}; + clear: function clear() { + storage.removeItem(name); + cache = {}; + } + }; }; };