Guest User

Untitled

a guest
Jun 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. // Event Emitter Class
  2. // ringo/events.js
  3.  
  4. export('EventEmitter');
  5.  
  6. function EventEmitter() {
  7.  
  8. }
  9.  
  10. EventEmitter.prototype = Object.create(Object.prototype,
  11.  
  12. event: {
  13. value: {}
  14. },
  15.  
  16. on: {
  17. value: function(event, callback) {
  18. if (event in this.events) {
  19. this.events[event] = [];
  20. }
  21.  
  22. this.events[event].push(callback);
  23. }
  24. },
  25.  
  26. emit: {
  27. value: function(event, args) {
  28. if ( ! (event in this.events)) {
  29. return;
  30. }
  31.  
  32. this.events[event].forEach(function(callback) {
  33. callback.apply(undefined, args);
  34. });
  35. }
  36. }
  37. }
  38.  
  39. // My Observable Class
  40. // Implements the event emitter interface
  41.  
  42. {EventEmitter} = require('ringo/events');
  43.  
  44. export('MyClass');
  45.  
  46. function MyClass() {
  47.  
  48. }
  49.  
  50. MyClass.prototype = Object.create(EventEmitter.prototype, {
  51.  
  52. myValue: { value: 'abc' }
  53. });
Add Comment
Please, Sign In to add comment