Requires

Provides

Observer.js

A class that tracks updates in input periodically and fires onChange event

License:
MIT-style license.
  1. 21
  2. 22
  3. 23
  4. 24
  5. 25
  6. 26
  7. 27
  8. 28
  9. 29
  10. 30
  11. 31
  12. 32
  13. 33
  14. 34
  15. 35
  16. 36
  17. 37
  18. 38
  19. 39
  20. 40
  21. 41
  22. 42
  23. 43
  24. 44
  25. 45
  26. 46
  27. 47
  28. 48
  29. 49
  30. 50
  31. 51
  32. 52
  33. 53
  34. 54
  35. 55
  36. 56
  37. 57
  38. 58
  39. 59
  40. 60
  41. 61
  42. 62
  43. 63
  44. 64
  45. 65
  46. 66
  47. 67
  48. 68
  49. 69
  50. 70
  51. 71
  52. 72
  53. 73
  54. 74
Observer = new Class({ Implements: [Options, Events], options: { periodical: false, delay: 1000 }, initialize: function(el, onFired, options){ this.element = $(el) || $$(el); this.addEvent('onFired', onFired); this.setOptions(options); this.bound = this.changed.bind(this); this.resume(); }, changed: function() { var value = this.element.get('value'); if (Object.equals(this.value, value)) return; this.clear(); this.value = value; this.timeout = this.onFired.delay(this.options.delay, this); }, setValue: function(value) { this.value = value; this.element.set('value', value); return this.clear(); }, onFired: function() { this.fireEvent('onFired', [this.value, this.element]); }, clear: function() { $clear(this.timeout || null); return this; }, pause: function(){ if (this.timer) $clear(this.timer); else this.element.removeEvent('keyup', this.bound); return this.clear(); }, resume: function(){ this.value = this.element.get('value'); if (this.options.periodical) this.timer = this.changed.periodical(this.options.periodical, this); else this.element.addEvent('keyup', this.bound); return this; } });