couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robertkowal...@apache.org
Subject fauxton commit: updated refs/heads/master to 38ad690
Date Tue, 04 Nov 2014 10:09:28 GMT
Repository: couchdb-fauxton
Updated Branches:
  refs/heads/master 3dec6696f -> 38ad69079


Added getter/setter utils.js methods for local storage

This is primarily proactive; there are only a couple of places in the code
that use local storage, but I wanted to nip it in the bud now and use
helpers instead.

Closes COUCHDB-2432


Project: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/commit/38ad6907
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/tree/38ad6907
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/diff/38ad6907

Branch: refs/heads/master
Commit: 38ad6907987d7089b61a68bf58d6e0b28be744f1
Parents: 3dec669
Author: Benjamin Keen <ben.keen@gmail.com>
Authored: Mon Nov 3 11:28:32 2014 -0800
Committer: Benjamin Keen <ben.keen@gmail.com>
Committed: Mon Nov 3 20:07:50 2014 -0800

----------------------------------------------------------------------
 app/addons/documents/routes-documents.js |  5 ++-
 app/core/tests/utilsSpec.js              | 46 +++++++++++++++++++++++++++
 app/core/utils.js                        | 28 ++++++++++++++++
 3 files changed, 76 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/38ad6907/app/addons/documents/routes-documents.js
----------------------------------------------------------------------
diff --git a/app/addons/documents/routes-documents.js b/app/addons/documents/routes-documents.js
index 56d0845..8b7f251 100644
--- a/app/addons/documents/routes-documents.js
+++ b/app/addons/documents/routes-documents.js
@@ -425,15 +425,14 @@ function(app, FauxtonAPI, Documents, Changes, Index, DocEditor, Databases,
Resou
     },
 
     setDocPerPageLimit: function (perPage) {
-      window.localStorage.setItem('fauxton:perpage', perPage);
+      app.utils.localStorageSet('fauxton:perpage', perPage);
     },
 
-
     getDocPerPageLimit: function (urlParams, perPage) {
       var storedPerPage = perPage;
 
       if (window.localStorage) {
-        storedPerPage = window.localStorage.getItem('fauxton:perpage');
+        storedPerPage = app.utils.localStorageGet('fauxton:perpage');
 
         if (!storedPerPage) {
           this.setDocPerPageLimit(perPage);

http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/38ad6907/app/core/tests/utilsSpec.js
----------------------------------------------------------------------
diff --git a/app/core/tests/utilsSpec.js b/app/core/tests/utilsSpec.js
new file mode 100644
index 0000000..8b8634f
--- /dev/null
+++ b/app/core/tests/utilsSpec.js
@@ -0,0 +1,46 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+define([
+  'api',
+  'testUtils',
+  'core/utils'
+], function (FauxtonAPI, testUtils, utils) {
+  var assert = testUtils.assert;
+
+  describe('Utils', function () {
+
+    describe('localStorage', function () {
+
+      it('Should get undefined when getting a non-existent key', function () {
+        assert.isUndefined(utils.localStorageGet('qwerty'));
+      });
+
+      it ('Should get value after setting it', function () {
+        var key = 'key1';
+        utils.localStorageSet(key, 1);
+        assert.equal(utils.localStorageGet(key), 1);
+      });
+
+      it ('Set and retrieve complex object', function () {
+        var key = 'key2',
+          obj = {
+            one: 1,
+            two: ['1', 'string', 3]
+          };
+        utils.localStorageSet(key, obj);
+        assert.deepEqual(utils.localStorageGet(key), obj);
+      });
+
+    });
+  });
+
+});

http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/38ad6907/app/core/utils.js
----------------------------------------------------------------------
diff --git a/app/core/utils.js b/app/core/utils.js
index d3699c7..3f3503e 100644
--- a/app/core/utils.js
+++ b/app/core/utils.js
@@ -96,6 +96,34 @@ function ($, _) {
       var testName = name || "";
       var checkforBad = testName.match(/[\$\-/,+-]/g);
       return (checkforBad !== null)?encodeURIComponent(name):name;
+    },
+
+    // a pair of simple local storage wrapper functions. These ward against problems getting
or
+    // setting (e.g. local storage full) and allow you to get/set complex data structures
+    localStorageSet: function (key, value) {
+      if (_.isObject(value) || _.isArray(value)) {
+        value = JSON.stringify(value);
+      }
+      var success = true;
+      try {
+        window.localStorage.setItem(key, value);
+      } catch (e) {
+        success = false;
+      }
+      return success;
+    },
+
+    localStorageGet: function (key) {
+      var data;
+      if (_.has(window.localStorage, key)) {
+        data = window.localStorage[key];
+        try {
+          return JSON.parse(data);
+        } catch (e) {
+          return data;
+        }
+      }
+      return data;
     }
   };
 


Mime
View raw message