Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…gument is not present
  • Loading branch information...
commit 2c01c699f5c34a39679e360e8a50fafda3ac353f 1 parent 290d90e
kangax authored

Showing 1 changed file with 40 additions and 37 deletions. Show diff stats Hide diff stats

  1. +40 37 class.watchable.js
77 class.watchable.js
@@ -26,46 +26,49 @@
26 26 * jdd.name = "Juriy"; // fires handler associated with "name", passing it: prop, oldValue, newValue
27 27 *
28 28 */
29   - Class.Watchable = Class.create({
30   - watch: function(prop, handler) {
31   - // isFunction will return true for regexp (but it's too ugly to fix it here)
32   - if (Object.isUndefined(prop) || !Object.isFunction(handler))
33   - throw new TypeError('Wrong arguments supplied');
  29 +Class.Watchable = Class.create({
  30 + watch: function(prop, handler) {
  31 + // isFunction will return true for regexp (but it's too ugly to fix it here)
  32 + if (Object.isUndefined(prop) || !Object.isFunction(handler))
  33 + throw new TypeError('Wrong arguments supplied');
34 34
35   - if (!this.__watchable) this.__watchable = { };
36   - var w = this.__watchable;
  35 + if (!this.__watchable) this.__watchable = { };
  36 + var w = this.__watchable;
37 37
38   - if (!w.clone) w.clone = { };
39   - w.clone[prop] = this[prop];
  38 + if (!w.clone) w.clone = { };
  39 + w.clone[prop] = this[prop];
40 40
41   - if (!w.handlers) w.handlers = { };
42   - if (!w.handlers[prop]) w.handlers[prop] = [ ];
43   - if (!w.handlers[prop].include(handler)) w.handlers[prop].push(handler);
  41 + if (!w.handlers) w.handlers = { };
  42 + if (!w.handlers[prop]) w.handlers[prop] = [ ];
  43 + if (!w.handlers[prop].include(handler)) w.handlers[prop].push(handler);
44 44
45   - if (!w.timer)
46   - w.timer = setInterval(this.__callback.bind(this), Class.Watchable.INTERVAL);
  45 + if (!w.timer)
  46 + w.timer = setInterval(this.__callback.bind(this), Class.Watchable.INTERVAL);
47 47
48   - return this;
49   - },
50   - unwatch: function(prop) {
51   - var w = this.__watchable;
52   - if (w.clone && w.clone[prop] && w.clone[prop].include(handler))
53   - w.clone[prop] = w.clone[prop].without(handler);
54   - return this;
55   - },
56   - __callback: function() {
57   - var oldValue, handlers, w = this.__watchable;
58   - for (var prop in w.clone) {
59   - if (w.clone[prop] != this[prop]) {
60   - oldValue = w.clone[prop];
61   - w.clone[prop] = this[prop];
62   - handlers = w.handlers[prop];
63   - for (var i=0, l=handlers.length; i<l; i++) {
64   - handlers[i].call(handlers[i], prop, oldValue, this[prop]);
65   - }
66   - }
67   - }
68   - }
69   - })
  48 + return this;
  49 + },
  50 + unwatch: function(prop, handler) {
  51 + var w = this.__watchable;
  52 + if (w.clone && w.clone[prop])
  53 + if (handler)
  54 + w.handlers[prop] = w.handlers[prop].without(handler);
  55 + else
  56 + w.handlers[prop] = [ ];
  57 + return this;
  58 + },
  59 + __callback: function() {
  60 + var oldValue, handlers, w = this.__watchable;
  61 + for (var prop in w.clone) {
  62 + if (w.clone[prop] != this[prop]) {
  63 + oldValue = w.clone[prop];
  64 + w.clone[prop] = this[prop];
  65 + handlers = w.handlers[prop];
  66 + for (var i=0, l=handlers.length; i<l; i++) {
  67 + handlers[i].call(handlers[i], prop, oldValue, this[prop]);
  68 + }
  69 + }
  70 + }
  71 + }
  72 +})
70 73
71   - Class.Watchable.INTERVAL = 100;
  74 +Class.Watchable.INTERVAL = 100;

0 comments on commit 2c01c69

Please sign in to comment.
Something went wrong with that request. Please try again.