Permalink
Browse files

Added persistent data storage functions

  • Loading branch information...
1 parent f5d46fe commit 873ed37f948113608c353919234dae463f1dc2d8 @fpillet fpillet committed May 2, 2012
Showing with 51 additions and 0 deletions.
  1. +3 −0 README.md
  2. +48 −0 persistentdata.js
View
@@ -54,4 +54,7 @@ number2hex(234234).toUpperCase(); // returns the string "392FA"
```
+## Persistent data
+Include the `persistentData.js` file to add functions to save and restore arbitrary data (not just strings) to persistent global tokens.
+Make sure you read the comments in the file for an example of use.
View
@@ -0,0 +1,48 @@
+/* Helper functions to save and restore arbitrary data of any size
+ * to a global token in iViewer. You will have to declare this global
+ * token in your GUI file and mark it as persistent.
+ *
+ * For first time initialization, the restorePersistentData() function
+ * takes a defaultValue value that is used if the data has never been
+ * saved yet.
+ *
+ * restorePersistentData() will call a callback function you give it and
+ * pass it the restored value (or the default value). Here is an example
+ * of use:
+ *
+CF.userMain = function() {
+ // restore an array we saved in the "SavedArray" global token,
+ // and the first-time contents of the array is [0,1,2]
+ restorePersistentData("SavedArray", [0,1,2], function(restoredValue) {
+ // we can now used the contents of the array we saved
+ });
+}
+
+// at any point in your code, you can save the contents of SomeGlobalArray
+// by doing:
+// savePersistentData("SavedArray", someArray);
+ *
+ * Note that the example above saves and restores an array, but you can save
+ * and restore any object (it can be more complex than a simple array).
+ *
+ */
+
+function savePersistentData(dataObject, token) {
+ CF.setToken(CF.GlobalTokensJoin, token, JSON.stringify(dataObject));
+}
+
+function restorePersistentData(token, defaultValue, callback) {
+ CF.getJoin(CF.GlobalTokensJoin, function(j,v,t) {
+ if (!t.hasOwnProperty(token) || t[token] === "") {
+ restored = defaultValue;
+ } else {
+ try {
+ restored = JSON.parse(t[token]);
+ }
+ catch(e) {
+ restored = defaultValue;
+ }
+ }
+ callback.apply(null, restored);
+ });
+}

0 comments on commit 873ed37

Please sign in to comment.