Skip to content

Instantly share code, notes, and snippets.

@strathmeyer
Created November 9, 2011 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strathmeyer/1352253 to your computer and use it in GitHub Desktop.
Save strathmeyer/1352253 to your computer and use it in GitHub Desktop.
amplify.js: Easily trigger a topic publish on a UI event
amplify.publishTo = function (topic) {
return function () { amplify.publish(topic); };
};
// Usage:
// $('#myButton').click( amplify.publishTo('myTopic') );
//
// Replaces:
// $('#myButton').click( function () { amplify.publish('myTopic') });
//
// meh. looks better on a single line, I guess. a little easier to read
// How about:
(function ($) {
$.fn.bindTopic = function(eventType, topic) {
return this.each(function() {
var $this = $(this);
$this.bind(eventType, function () {
amplify.publish(topic);
});
});
};
})( jQuery );
// Usage:
// $('#myButton').bindTopic('click', 'myTopic');
// TODO:
// - add 'data' parameter?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment