Reputation: 4098
How can I listen to value set on a variable or object property without losing other listeners.
for example
var myModel;
myModel = 10;
var myListener1 = function(oldVal, newVal) {
//do stuff
}
var myListener2 = function(oldVal, newVal) {
//do other stuff
}
here I want myListener1 and myListener2 to be called whenever a value is set for variable myModel. Later in other functions might also want to add another listener to myModel on setter so it shouldn't override existing ones.
I know there should be a way to do it with Object.defineProperty(). Also it would be good to have a solution cross browser for IE8+.
Upvotes: 1
Views: 1895
Reputation: 1848
For the following method, you would have to use an object with a property, but it works.
// this is very similar to using Object.watch()
// instead we attach multiple listeners
var myModel = (function () {
var actualValue,
interceptors = [];
function callInterceptors(newValue) {
for (var i = 0; i < interceptors.length; i += 1) {
interceptors[i](newValue);
}
}
return {
get value() {
// user never has access to the private variable "actualValue"
// we can control what they get back from saying "myModel.value"
return actualValue;
},
set value(newValue) {
callInterceptors(newValue);
actualValue = newValue;
},
listen : function (fn) {
if (typeof fn === 'function') {
interceptors.push(fn);
}
}
};
}());
// add a listener
myModel.listen(function (passedValue) {
alert('You tried to set myValue to ' + passedValue);
});
// add another listener
myModel.listen(function (passedValue) {
alert('AAARRG! Why did you modify that value to ' + passedValue + '?!?!');
});
// our functions are called when we
// modify our value
myModel.value = 10;
Upvotes: 6
Reputation: 4271
only change the value of myModel
through a function so you can run the listener functions after/before the change.
var myModel = 10,
myListener1 = function(oldVal, newVal) {
// do stuff
},
myListener2 = function(oldVal, newVal) {
// do other stuff
},
changeMyModel = function (value) {
myListener1(myModel, value);
myListener2(myModel, value);
myModel = value;
// do more stuff
};
// changeMyModel(99) === myModel; true
OR
var change = function (value) {
myListener1(myModel, value);
myListener2(myModel, value);
return value;
};
// myModel = change(45);
Upvotes: 0