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