From 7bbcbe18619c255dec42f092c40bb076ee20f031 Mon Sep 17 00:00:00 2001 From: Vince Date: Wed, 19 Jun 2013 01:51:53 +0200 Subject: [PATCH] Review comments --- js/rendering/FrameRenderer.js | 6 ++++- js/utils/UserSettings.js | 45 +++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/js/rendering/FrameRenderer.js b/js/rendering/FrameRenderer.js index 75223860..99d30131 100644 --- a/js/rendering/FrameRenderer.js +++ b/js/rendering/FrameRenderer.js @@ -145,7 +145,11 @@ var pixelWidth = col * this.dpi + this.gridStrokeWidth * (col - 1); var pixelHeight = row * this.dpi + this.gridStrokeWidth * (row - 1); - var canvas = pskl.CanvasUtils.createCanvas(pixelWidth, pixelHeight, ["canvas", this.className]); + var classes = ['canvas']; + if (this.className) { + classes.push(this.className); + } + var canvas = pskl.CanvasUtils.createCanvas(pixelWidth, pixelHeight, classes); this.container.append(canvas); diff --git a/js/utils/UserSettings.js b/js/utils/UserSettings.js index 73946d86..89359bee 100644 --- a/js/utils/UserSettings.js +++ b/js/utils/UserSettings.js @@ -21,17 +21,18 @@ * value if not defined yet. */ get : function (key) { - this.checKeyValidity_(key); - if (key in this.cache_) { - return this.cache_[key]; + this.checkKeyValidity_(key); + if (!(key in this.cache_)) { + this.cache_[key] = + this.readFromLocalStorage_(key) || this.readFromDefaults_(key); } - return this.get_(key); + return this.cache_[key]; }, set : function (key, value) { - this.checKeyValidity_(key); + this.checkKeyValidity_(key); this.cache_[key] = value; - this.set_(key, value); + this.writeToLocalStorage_(key, value); $.publish(Events.USER_SETTINGS_CHANGED, [key, value]); }, @@ -39,14 +40,10 @@ /** * @private */ - get_ : function(key) { + readFromLocalStorage_ : function(key) { var value = window.localStorage[key]; - if (value === undefined) { - value = this.KEY_TO_DEFAULT_VALUE_MAP_[key]; - } - else { - var entry = JSON.parse(value); - value = entry.jsonValue; + if (typeof value != "undefined") { + value = JSON.parse(value); } return value; }, @@ -54,20 +51,26 @@ /** * @private */ - set_ : function(key, value) { - var entry = { 'jsonValue': value }; - window.localStorage[key] = JSON.stringify(entry); + writeToLocalStorage_ : function(key, value) { + // TODO(grosbouddha): Catch storage exception here. + window.localStorage[key] = JSON.stringify(value); }, /** * @private */ - checKeyValidity_ : function(key) { - if(key in this.KEY_TO_DEFAULT_VALUE_MAP_) { - return true; + readFromDefaults_ : function (key) { + return this.KEY_TO_DEFAULT_VALUE_MAP_[key]; + }, + + /** + * @private + */ + checkKeyValidity_ : function(key) { + if(!(key in this.KEY_TO_DEFAULT_VALUE_MAP_)) { + // TODO(grosbouddha): Define error catching strategy and throw exception from here. + console.log("UserSettings key <"+ key +"> not find in supported keys."); } - console.log("UserSettings key <"+ key +"> not find in supported keys."); - return false; } }; })(); \ No newline at end of file