Created
November 9, 2011 17:51
-
-
Save strathmeyer/1352253 to your computer and use it in GitHub Desktop.
amplify.js: Easily trigger a topic publish on a UI event
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
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