Created
October 26, 2012 01:10
-
-
Save ElemarJR/3956426 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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