Browse files

unwatch now accepts particular handler to unwatch or purges all if ar…

…gument is not present
  • Loading branch information...
1 parent 290d90e commit 2c01c699f5c34a39679e360e8a50fafda3ac353f kangax committed Apr 30, 2008
Showing with 40 additions and 37 deletions.
  1. +40 −37 class.watchable.js
View
77 class.watchable.js
@@ -26,46 +26,49 @@
* jdd.name = "Juriy"; // fires handler associated with "name", passing it: prop, oldValue, newValue
*
*/
- Class.Watchable = Class.create({
- watch: function(prop, handler) {
- // isFunction will return true for regexp (but it's too ugly to fix it here)
- if (Object.isUndefined(prop) || !Object.isFunction(handler))
- throw new TypeError('Wrong arguments supplied');
+Class.Watchable = Class.create({
+ watch: function(prop, handler) {
+ // isFunction will return true for regexp (but it's too ugly to fix it here)
+ if (Object.isUndefined(prop) || !Object.isFunction(handler))
+ throw new TypeError('Wrong arguments supplied');
- if (!this.__watchable) this.__watchable = { };
- var w = this.__watchable;
+ if (!this.__watchable) this.__watchable = { };
+ var w = this.__watchable;
- if (!w.clone) w.clone = { };
- w.clone[prop] = this[prop];
+ if (!w.clone) w.clone = { };
+ w.clone[prop] = this[prop];
- if (!w.handlers) w.handlers = { };
- if (!w.handlers[prop]) w.handlers[prop] = [ ];
- if (!w.handlers[prop].include(handler)) w.handlers[prop].push(handler);
+ if (!w.handlers) w.handlers = { };
+ if (!w.handlers[prop]) w.handlers[prop] = [ ];
+ if (!w.handlers[prop].include(handler)) w.handlers[prop].push(handler);
- if (!w.timer)
- w.timer = setInterval(this.__callback.bind(this), Class.Watchable.INTERVAL);
+ if (!w.timer)
+ w.timer = setInterval(this.__callback.bind(this), Class.Watchable.INTERVAL);
- return this;
- },
- unwatch: function(prop) {
- var w = this.__watchable;
- if (w.clone && w.clone[prop] && w.clone[prop].include(handler))
- w.clone[prop] = w.clone[prop].without(handler);
- return this;
- },
- __callback: function() {
- var oldValue, handlers, w = this.__watchable;
- for (var prop in w.clone) {
- if (w.clone[prop] != this[prop]) {
- oldValue = w.clone[prop];
- w.clone[prop] = this[prop];
- handlers = w.handlers[prop];
- for (var i=0, l=handlers.length; i<l; i++) {
- handlers[i].call(handlers[i], prop, oldValue, this[prop]);
- }
- }
- }
- }
- })
+ return this;
+ },
+ unwatch: function(prop, handler) {
+ var w = this.__watchable;
+ if (w.clone && w.clone[prop])
+ if (handler)
+ w.handlers[prop] = w.handlers[prop].without(handler);
+ else
+ w.handlers[prop] = [ ];
+ return this;
+ },
+ __callback: function() {
+ var oldValue, handlers, w = this.__watchable;
+ for (var prop in w.clone) {
+ if (w.clone[prop] != this[prop]) {
+ oldValue = w.clone[prop];
+ w.clone[prop] = this[prop];
+ handlers = w.handlers[prop];
+ for (var i=0, l=handlers.length; i<l; i++) {
+ handlers[i].call(handlers[i], prop, oldValue, this[prop]);
+ }
+ }
+ }
+ }
+})
- Class.Watchable.INTERVAL = 100;
+Class.Watchable.INTERVAL = 100;

0 comments on commit 2c01c69

Please sign in to comment.