Advertisement
Guest User

jsObservables

a guest
Oct 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var observableText = {
  2.     observers: [],
  3.     text: "",
  4.     notifyAll: function(o){
  5.         for (var j = 0; j < this.observers.length; j++){
  6.           this.observers[j].update(this.text);
  7.         }
  8.     },
  9.     addObserver: function(o){
  10.         this.observers.push(o);
  11.     },
  12.     updateText: function(t){
  13.         this.text = t;
  14.         this.notifyAll();
  15.     }
  16. };
  17.  
  18. var displayText = {
  19.     text: "",
  20.     update: function(t){
  21.         this.text = t;
  22.         console.log(this.text);
  23.     }
  24. }
  25. observableText.addObserver(displayText);
  26. observableText.updateText("updated once");
  27. observableText.updateText("updated twice");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement