Adding callbacks to Twitter Bootstrap’s Javascript plugins

popover_callback

The minute I first saw Twitter’s Bootstrap I knew I was going to use it continuously. Either for prototyping or building a site in a short period of time, Twitter Bootstrap has an incredible amount of features/plugins that will ensure your website/app will look great under any browser/device.

However, after using some of the Javascript plugins, I notice some of them don’t have a callback function, which is pretty handy when you want to do more custom stuffs. Luckily, after doing some research I found out that in Javascript a callback function can be added through the function property “prototype”.

So imagine you want to do something after a popover is opened. To add a callback function do the following. Notice that you can call it whatever you want:

var pt = $.fn.popover.Constructor.prototype.show;
$.fn.popover.Constructor.prototype.show = function () {
pt.call(this);
if (this.options.afterShowed) {
this.options.afterShowed();
}
}

Once you have done that, you can call the callback function when initialising your popover object:

$('#example').popover({
afterShowed: function() {
// do something
}
});

Look at the demo here.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">