Made observable object sealed after creation

This commit is contained in:
Borys Levytskyi
2015-04-04 19:23:46 +03:00
parent 9744f88c3d
commit ba284789ba
5 changed files with 51 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
(function (core) {
"use strict";
var di = new core.Container();

View File

@@ -11,7 +11,7 @@ app.run(function() {
},
'em': function() {
var cfg = app.get('cmdConfig');
cfg.emphasizeBytes = !cfg.cmdConfig.emphasizeBytes;
cfg.emphasizeBytes = !cfg.emphasizeBytes;
}
});

View File

@@ -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) {

View File

@@ -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;
}

View File

@@ -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);