Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. var observer = {
  2. subscribers: {},
  3. on: function(type, fn){
  4. //check if exist, if not, define
  5. this.subscribers[type] = [];
  6. this.subscribers[type].push({fn: fn})
  7. },
  8. trigger: function(type){
  9. for(var i=0; i<this.subscribers[type].length; i++){
  10. this.subscribers[type][i].fn.call();
  11. }
  12. },
  13. remove: function(type, fn){
  14. for(var i=0; i<this.subscribers[type].length; i++){
  15. if(this.subscribers[type][i].fn === fn){
  16. this.subscribers[type].splice(i,1);
  17. }
  18. }
  19. }
  20. }
  21.  
  22.  
  23. var helloFn = function(){
  24. console.log('Hello!');
  25. }
  26.  
  27. observer.on('sayHello', helloFn);
  28. observer.trigger('sayHello');
  29. observer.remove('sayHello', helloFn);
  30. observer.trigger('sayHello');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement