annotate src/DML/MainVisBundle/Resources/assets/marionette/modules/DataModule/DataModule.01-Storage.js @ 1:f38015048f48 tip

Added GPL
author Daniel Wolff
date Sat, 13 Feb 2016 20:43:38 +0100
parents 493bcb69166c
children
rev   line source
Daniel@0 1 "use strict";
Daniel@0 2 /*
Daniel@0 3 * Storage conventions
Daniel@0 4 * All data are saved with keys of the following format to keep things ordered: dml.moduleName.key
Daniel@0 5 */
Daniel@0 6
Daniel@0 7 App.module("DataModule.Storage", function(Storage, App, Backbone, Marionette, $, _, Logger) {
Daniel@0 8 // Prevent auto start
Daniel@0 9 //Storage.startWithParent = false;
Daniel@0 10
Daniel@0 11 // Define private variables
Daniel@0 12 var logger = null;
Daniel@0 13
Daniel@0 14 /**
Daniel@0 15 * Initialization checker
Daniel@0 16 */
Daniel@0 17 var assertModuleIsInitialized = function() {
Daniel@0 18 if (!logger) {
Daniel@0 19 throw "Storage has not been initialized";
Daniel@0 20 }
Daniel@0 21 };
Daniel@0 22
Daniel@0 23 /**
Daniel@0 24 * Module initializer
Daniel@0 25 *
Daniel@0 26 */
Daniel@0 27 Storage.addInitializer(function(){
Daniel@0 28 logger = Logger.get("DataModule.Storage");
Daniel@0 29 // logger.setLevel(Logger.DEBUG);
Daniel@0 30
Daniel@0 31 logger.debug("Begin init");
Daniel@0 32 logger.debug("End init");
Daniel@0 33 });
Daniel@0 34
Daniel@0 35 /**
Daniel@0 36 * @memberOf App.Storage
Daniel@0 37 *
Daniel@0 38 * @returns {Boolean} true when success, false when failure (e.g. local storage is full or disabled)
Daniel@0 39 */
Daniel@0 40 Storage.setStrCache = function(module, key, value) {
Daniel@0 41 assertModuleIsInitialized();
Daniel@0 42 var moduleName = _.isObject(module) ? module.moduleName : "";
Daniel@0 43 if (!moduleName) {
Daniel@0 44 moduleName = "";
Daniel@0 45 }
Daniel@0 46 if (value === undefined) {
Daniel@0 47 localStorage && localStorage.removeItem("dml." + moduleName + "." + key);
Daniel@0 48 return true;
Daniel@0 49 } else if (_.isString(value)) {
Daniel@0 50 try {
Daniel@0 51 localStorage && localStorage.setItem("dml." + moduleName + "." + key, value);
Daniel@0 52 logger.info("Set string cache", moduleName, key, value);
Daniel@0 53 return true;
Daniel@0 54 } catch (e) {
Daniel@0 55 var maxOutputLength = 100;
Daniel@0 56 logger.error("Set string cache failed:", moduleName, "→", key, "=", value.slice(0, maxOutputLength) + (value.length > maxOutputLength ? "..." : ""));
Daniel@0 57 //logger.error("Set string cache failed:", moduleName, "→", key, "=", value);
Daniel@0 58 return false;
Daniel@0 59 }
Daniel@0 60 } else {
Daniel@0 61 throw _.str.sprintf("Storage.setStrCache only accepts string for value, %s given: ", typeof value, value)
Daniel@0 62 }
Daniel@0 63 };
Daniel@0 64
Daniel@0 65 /**
Daniel@0 66 * @memberOf App.Storage
Daniel@0 67 *
Daniel@0 68 * @returns {String|undefined}
Daniel@0 69 */
Daniel@0 70 Storage.getStrCache = function(module, key) {
Daniel@0 71 assertModuleIsInitialized();
Daniel@0 72 var moduleName = _.isObject(module) ? module.moduleName : "";
Daniel@0 73 if (!moduleName) {
Daniel@0 74 moduleName = "";
Daniel@0 75 }
Daniel@0 76 var value = localStorage ? localStorage.getItem("dml." + moduleName + "." + key) : null;
Daniel@0 77 if (value === null) {
Daniel@0 78 value = undefined;
Daniel@0 79 }
Daniel@0 80 logger.info("Get string cache", module, key, value);
Daniel@0 81 return value;
Daniel@0 82 };
Daniel@0 83
Daniel@0 84 /**
Daniel@0 85 * @memberOf App.Storage
Daniel@0 86 *
Daniel@0 87 * @returns {Boolean} true when success, false when failure (e.g. local storage is full or disabled)
Daniel@0 88 */
Daniel@0 89 Storage.setObjCache = function(module, key, value) {
Daniel@0 90 if (!(_.isUndefined(value) || _.isNull(value) || (_.isObject(value) && !_.isFunction(value)))) {
Daniel@0 91 throw _.str.sprintf("Storage.setObjCache can only acccept object, got %s", value);
Daniel@0 92 }
Daniel@0 93 var str = JSON.stringify(value);
Daniel@0 94 return Storage.setStrCache(module, key, str);
Daniel@0 95 };
Daniel@0 96
Daniel@0 97 /**
Daniel@0 98 * @memberOf App.Storage
Daniel@0 99 *
Daniel@0 100 * @returns {Object|undefined}
Daniel@0 101 */
Daniel@0 102 Storage.getObjCache = function(module, key) {
Daniel@0 103 assertModuleIsInitialized();
Daniel@0 104 var value = localStorage ? localStorage.getItem("dml." + module.moduleName + "." + key) : null;
Daniel@0 105 if (value === null) {
Daniel@0 106 return undefined;
Daniel@0 107 }
Daniel@0 108 if (value === "null") {
Daniel@0 109 return null;
Daniel@0 110 }
Daniel@0 111 try {
Daniel@0 112 var value = JSON.parse(value);
Daniel@0 113 return value;
Daniel@0 114 } catch (e) {
Daniel@0 115 return null;
Daniel@0 116 }
Daniel@0 117 };
Daniel@0 118
Daniel@0 119 }, Logger);