Daniel@0: "use strict"; Daniel@0: /* Daniel@0: * Storage conventions Daniel@0: * All data are saved with keys of the following format to keep things ordered: dml.moduleName.key Daniel@0: */ Daniel@0: Daniel@0: App.module("DataModule.Storage", function(Storage, App, Backbone, Marionette, $, _, Logger) { Daniel@0: // Prevent auto start Daniel@0: //Storage.startWithParent = false; Daniel@0: Daniel@0: // Define private variables Daniel@0: var logger = null; Daniel@0: Daniel@0: /** Daniel@0: * Initialization checker Daniel@0: */ Daniel@0: var assertModuleIsInitialized = function() { Daniel@0: if (!logger) { Daniel@0: throw "Storage has not been initialized"; Daniel@0: } Daniel@0: }; Daniel@0: Daniel@0: /** Daniel@0: * Module initializer Daniel@0: * Daniel@0: */ Daniel@0: Storage.addInitializer(function(){ Daniel@0: logger = Logger.get("DataModule.Storage"); Daniel@0: // logger.setLevel(Logger.DEBUG); Daniel@0: Daniel@0: logger.debug("Begin init"); Daniel@0: logger.debug("End init"); Daniel@0: }); Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.Storage Daniel@0: * Daniel@0: * @returns {Boolean} true when success, false when failure (e.g. local storage is full or disabled) Daniel@0: */ Daniel@0: Storage.setStrCache = function(module, key, value) { Daniel@0: assertModuleIsInitialized(); Daniel@0: var moduleName = _.isObject(module) ? module.moduleName : ""; Daniel@0: if (!moduleName) { Daniel@0: moduleName = ""; Daniel@0: } Daniel@0: if (value === undefined) { Daniel@0: localStorage && localStorage.removeItem("dml." + moduleName + "." + key); Daniel@0: return true; Daniel@0: } else if (_.isString(value)) { Daniel@0: try { Daniel@0: localStorage && localStorage.setItem("dml." + moduleName + "." + key, value); Daniel@0: logger.info("Set string cache", moduleName, key, value); Daniel@0: return true; Daniel@0: } catch (e) { Daniel@0: var maxOutputLength = 100; Daniel@0: logger.error("Set string cache failed:", moduleName, "→", key, "=", value.slice(0, maxOutputLength) + (value.length > maxOutputLength ? "..." : "")); Daniel@0: //logger.error("Set string cache failed:", moduleName, "→", key, "=", value); Daniel@0: return false; Daniel@0: } Daniel@0: } else { Daniel@0: throw _.str.sprintf("Storage.setStrCache only accepts string for value, %s given: ", typeof value, value) Daniel@0: } Daniel@0: }; Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.Storage Daniel@0: * Daniel@0: * @returns {String|undefined} Daniel@0: */ Daniel@0: Storage.getStrCache = function(module, key) { Daniel@0: assertModuleIsInitialized(); Daniel@0: var moduleName = _.isObject(module) ? module.moduleName : ""; Daniel@0: if (!moduleName) { Daniel@0: moduleName = ""; Daniel@0: } Daniel@0: var value = localStorage ? localStorage.getItem("dml." + moduleName + "." + key) : null; Daniel@0: if (value === null) { Daniel@0: value = undefined; Daniel@0: } Daniel@0: logger.info("Get string cache", module, key, value); Daniel@0: return value; Daniel@0: }; Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.Storage Daniel@0: * Daniel@0: * @returns {Boolean} true when success, false when failure (e.g. local storage is full or disabled) Daniel@0: */ Daniel@0: Storage.setObjCache = function(module, key, value) { Daniel@0: if (!(_.isUndefined(value) || _.isNull(value) || (_.isObject(value) && !_.isFunction(value)))) { Daniel@0: throw _.str.sprintf("Storage.setObjCache can only acccept object, got %s", value); Daniel@0: } Daniel@0: var str = JSON.stringify(value); Daniel@0: return Storage.setStrCache(module, key, str); Daniel@0: }; Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.Storage Daniel@0: * Daniel@0: * @returns {Object|undefined} Daniel@0: */ Daniel@0: Storage.getObjCache = function(module, key) { Daniel@0: assertModuleIsInitialized(); Daniel@0: var value = localStorage ? localStorage.getItem("dml." + module.moduleName + "." + key) : null; Daniel@0: if (value === null) { Daniel@0: return undefined; Daniel@0: } Daniel@0: if (value === "null") { Daniel@0: return null; Daniel@0: } Daniel@0: try { Daniel@0: var value = JSON.parse(value); Daniel@0: return value; Daniel@0: } catch (e) { Daniel@0: return null; Daniel@0: } Daniel@0: }; Daniel@0: Daniel@0: }, Logger);