Greg Pettit
Greg Pettit

Reputation: 10830

Bootbox 'loaded' callback

Does Bootbox.js have a method for invoking a function upon successfully showing a dialog?

I had originally planned to use it to switch a hidden form from elsewhere in the page into the Bootbox, but that can't be done without a callback or callback analogy. Then I devised a way of moving the hidden form in, and in a forehead-smacking cringe-worthy moment I realized that I still needed a callback (or analogy) to now reveal the form (remove the 'hide' class, add the 'in' class).

The documentation does not seem to show a built in property that will allow me to do this (bootbox.init shows promise, but I don't fully understand it, it is not documented with examples, and it seems to be global. I use many bootboxes). Is there another way I can do this? Does Bootbox publish an event of some sort?

Upvotes: 2

Views: 7688

Answers (2)

Tieson T.
Tieson T.

Reputation: 21231

.init() is called on a specific dialog, as I showed someone else here: https://jsfiddle.net/Lu1wp3nn/

A simplified example:

bootbox
    .alert('Your message')
    .init(function() { 
        /* do something */ 
    });

Because bootbox.js is just a wrapper over Bootstrap's modals, you have access to the modal events that Bootstrap defines. You're after shown.bs.modal (or show.bs.modal if you want to do something just prior to the dialog being shown).

Here's an example, adapted from the Bootstrap docs:

bootbox
    .alert('Your message')
    .on('shown.bs.modal', function (e) {
        // do something...
    })

Upvotes: 8

Elton da Costa
Elton da Costa

Reputation: 1277

To general use:

bootbox.alert|confirm({
  "title": lang['EMBED'],
  "message": content
  }).on("shown.bs.modal", function(e) {
   alert()
 });

Upvotes: 1

Related Questions