Last active
November 4, 2015 17:29
-
-
Save Komock/aa93b8a9bf10fed41065 to your computer and use it in GitHub Desktop.
Jquery observer pattern (single object module)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){ | |
//-- Observer | |
var o = $({}); | |
$.each({ | |
trigger: 'publish', | |
on: 'subscribe', | |
off: 'unsubscribe' | |
}, function(key, val){ | |
jQuery[val] = function(){ | |
o[key].apply( o, arguments ); | |
}; | |
}); | |
// Application | |
var app = { | |
customModule: function(){ | |
var data = 'Some Data!'; | |
// Publish event | |
console.log('Publish event'); | |
$.publish( 'custom_Event', data ); | |
}, | |
events: function(){ | |
// Subscribe on custom event | |
$.subscribe('custom_Event', function(e, data){ | |
$('.box').text(data); | |
console.log('Action on event!'); | |
}); | |
}, | |
init: function(){ | |
app.events(); | |
app.customModule(); | |
} | |
}; | |
app.init(); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment