Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var observer = {
- subscribers: {},
- on: function(type, fn){
- //check if exist, if not, define
- this.subscribers[type] = [];
- this.subscribers[type].push({fn: fn})
- },
- trigger: function(type){
- for(var i=0; i<this.subscribers[type].length; i++){
- this.subscribers[type][i].fn.call();
- }
- },
- remove: function(type, fn){
- for(var i=0; i<this.subscribers[type].length; i++){
- if(this.subscribers[type][i].fn === fn){
- this.subscribers[type].splice(i,1);
- }
- }
- }
- }
- var helloFn = function(){
- console.log('Hello!');
- }
- observer.on('sayHello', helloFn);
- observer.trigger('sayHello');
- observer.remove('sayHello', helloFn);
- observer.trigger('sayHello');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement