mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-23 13:12:42 +01:00
introduced observable
This commit is contained in:
@@ -1,16 +1,18 @@
|
||||
(function(){
|
||||
var observable = {};
|
||||
(function(core){
|
||||
function ObservableObject () {
|
||||
this.executionHandlers = [];
|
||||
}
|
||||
|
||||
observable.create = function(definition){
|
||||
var obj = new bindr.ObservableObject();
|
||||
ObservableObject.create = function(definition){
|
||||
var obj = new ObservableObject();
|
||||
for(var property in definition){
|
||||
if(!definition.hasOwnProperty(property)){
|
||||
continue;
|
||||
}
|
||||
|
||||
Object.defineProperty(obj, property, {
|
||||
get:bindr.ObservableObject.createGetter(property),
|
||||
set:bindr.ObservableObject.createSetter(property)
|
||||
get:ObservableObject.createGetter(property),
|
||||
set:ObservableObject.createSetter(property)
|
||||
});
|
||||
|
||||
obj[property] = definition[property];
|
||||
@@ -18,34 +20,30 @@
|
||||
return obj;
|
||||
};
|
||||
|
||||
observable.ObservableObject = function() {
|
||||
this.executionHandlers = [];
|
||||
};
|
||||
|
||||
observable.ObservableObject.createGetter = function (propertyName){
|
||||
ObservableObject.createGetter = function (propertyName){
|
||||
return function(){
|
||||
return this["_" + propertyName];
|
||||
}
|
||||
};
|
||||
|
||||
observable.ObservableObject.createSetter = function(propertyName){
|
||||
ObservableObject.createSetter = function(propertyName){
|
||||
return function(value){
|
||||
this["_" + propertyName] = value;
|
||||
this.notifyPropertyChanged(propertyName, value);
|
||||
}
|
||||
};
|
||||
|
||||
observable.ObservableObject.prototype.observe = function (handler){
|
||||
ObservableObject.prototype.observe = function (handler){
|
||||
var handlers = this.executionHandlers;
|
||||
var index = handlers.push(handler);
|
||||
return function () { handlers.splice(1, index); }
|
||||
};
|
||||
|
||||
observable.ObservableObject.prototype.notifyPropertyChanged = function(propertyName, value){
|
||||
ObservableObject.prototype.notifyPropertyChanged = function(propertyName, value){
|
||||
this.executionHandlers.forEach(function(h){
|
||||
h(propertyName, value);
|
||||
});
|
||||
};
|
||||
|
||||
window.core.observable = observable;
|
||||
});
|
||||
core.ObservableObject = ObservableObject;
|
||||
})(window.core);
|
||||
Reference in New Issue
Block a user