From ba284789ba0f6394afedd2a5f910a74b87401da7 Mon Sep 17 00:00:00 2001 From: Borys Levytskyi Date: Sat, 4 Apr 2015 19:23:46 +0300 Subject: [PATCH] Made observable object sealed after creation --- app/app.js | 1 + app/commandsCatalog.js | 2 +- app/services.js | 27 +++++++++++++++++++++++++++ core/di.js | 4 ++-- core/observable.js | 28 ++++++++++++++++++++-------- 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/app/app.js b/app/app.js index f0b1381..4f4ff19 100644 --- a/app/app.js +++ b/app/app.js @@ -1,4 +1,5 @@ (function (core) { + "use strict"; var di = new core.Container(); diff --git a/app/commandsCatalog.js b/app/commandsCatalog.js index 180e80e..0aa38c8 100644 --- a/app/commandsCatalog.js +++ b/app/commandsCatalog.js @@ -11,7 +11,7 @@ app.run(function() { }, 'em': function() { var cfg = app.get('cmdConfig'); - cfg.emphasizeBytes = !cfg.cmdConfig.emphasizeBytes; + cfg.emphasizeBytes = !cfg.emphasizeBytes; } }); diff --git a/app/services.js b/app/services.js index 446da4a..06e4bff 100644 --- a/app/services.js +++ b/app/services.js @@ -4,6 +4,33 @@ app.set('is', core.is); app.set('should', core.should); app.set('bindr', core.bindr); + + // Save config in local store + app.run(function() { + var cfg = app.get('cmdConfig'); + var storeKey = 'cmdConfig'; + + load(); + + cfg.observe(function(property, value){ + save(); + }); + + function save() { + localStorage.setItem(storeKey, JSON.stringify(cfg.store())); + } + + function load() { + var json = localStorage.getItem(storeKey), stored; + if(core.is.string(json)) { + stored = JSON.parse(json); + for(var key in stored) { + cfg[key] = stored[key]; + } + } + } + }); + /* var template = { compile: function (template) { diff --git a/core/di.js b/core/di.js index ca9fbcc..04146b5 100644 --- a/core/di.js +++ b/core/di.js @@ -38,7 +38,7 @@ this.resolutionStack.unshift(name); - console.log('\tresolution path:' + this.resolutionStack.join(' < ')); + // console.log('\tresolution path:' + this.resolutionStack.join(' < ')); var reg = this.store[name]; if(reg == null) { @@ -51,7 +51,7 @@ this.resolutionStack.shift(); - console.log('\tresolution path:' + this.resolutionStack.join(' < ')); + // console.log('\tresolution path:' + this.resolutionStack.join(' < ')); return reg.resolved; } diff --git a/core/observable.js b/core/observable.js index 12bae9e..125b5c3 100644 --- a/core/observable.js +++ b/core/observable.js @@ -2,11 +2,13 @@ var is = core.is; function ObservableObject () { - this.executionHandlers = []; + this.$store = {}; + this.$executionHandlers = []; } ObservableObject.create = function(definition){ var obj = new ObservableObject(); + for(var property in definition){ if(!definition.hasOwnProperty(property)){ continue; @@ -19,18 +21,19 @@ obj[property] = definition[property]; } - return obj; + + return Object.seal(obj); }; - ObservableObject.createGetter = function (propertyName){ + ObservableObject.createGetter = function (propertyName, store){ return function(){ - return this["_" + propertyName]; + return this.$store[propertyName]; } }; - ObservableObject.createSetter = function(propertyName){ + ObservableObject.createSetter = function(propertyName, store){ return function(value){ - this["_" + propertyName] = value; + this.$store[propertyName] = value; this.notifyPropertyChanged(propertyName, value); } }; @@ -52,16 +55,25 @@ return; } - var handlers = this.executionHandlers; + var handlers = this.$executionHandlers; var index = handlers.push(func); return function () { handlers.splice(1, index); } }; ObservableObject.prototype.notifyPropertyChanged = function(propertyName, value){ - this.executionHandlers.forEach(function(h){ + this.$executionHandlers.forEach(function(h){ h(propertyName, value); }); }; + ObservableObject.prototype.store = function() { + return this.$store; + }; + + ObservableObject.prototype.keys = function() { + return Object.keys(this.$store); + }; + core.ObservableObject = ObservableObject; + })(window.core); \ No newline at end of file