Skip to content

Instantly share code, notes, and snippets.

@ElemarJR
Created October 26, 2012 01:10
Show Gist options
  • Select an option

  • Save ElemarJR/3956426 to your computer and use it in GitHub Desktop.

Select an option

Save ElemarJR/3956426 to your computer and use it in GitHub Desktop.
var MyClass = function(data) {
var dataChanging = [],
dataChanged = [];
this.data = function(value) {
if (value !== undefined && value !== data) {
for (var i = 0; i < dataChanging.length; i++) {
if (!dataChanging[i](this, value)) {
return data;
}
};
data = value;
for (var i = 0; i < dataChanging.length; i++) {
dataChanged[i](this, value);
};
}
return data;
}
this.onDataChanging = function (callback) {
dataChanging.push(callback);
}
this.onDataChanged = function(callback) {
dataChanged.push(callback);
}
}
var c = new MyClass(5);
c.onDataChanging(function(sender, newValue) {
if (newValue > 10)
{
console.log("'data' should be <= 10");
return false;
}
return true;
});
c.onDataChanged(function(sender, newValue) {
console.log("'data' changed to " + newValue);
});
c.data(6);
c.data(11);
console.log(c.data());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment