简体   繁体   中英

How to get the modal dialog instance through the Bootstrap programmatic API?

The scenario I'm trying to solve is to disable that the escape-button closes the dialog AFTER the modal has been instaniated (the dialog is set to a loading state). So in other words after I have instaniated my modal like this:

(this.$el).modal("show");

The user presses a submit button and the dialog is set to a loading state and I want to disable the escape-button since the user should not be able to close the dialog in this state.

I have tried this:

(this.$el).modal({ keyboard: false });

But it does not work, it seems that Bootstrap only reads those options when it instaniates the modal dialog...

So my question is if it is possible to get a hold of the actual bootstrap modal-instance to be able to change the options-object? According to the documentation it should be possible (or have I misunderstood the docs?), but I cannot figure out how.

Here is what it says in the documentation ( http://getbootstrap.com/javascript/ ): If you'd like to get a particular plugin instance, retrieve it directly from an element:

$('[rel="popover"]').data('popover').

Any ideas?

Ok, I figured out how to get ahold of the modal dialog instance after some experimentation:

var bootstrapModalInstance = this.$el.data("bs.modal");

And then I could set the options on the instance like this:

bootstrapModalInstance.options.keyboard = !this.model.isSyncing;

Sadly enough, this did not solve the problem since the escape-key-event-listener is setup during the modal instaniation like this:

From bootstrap.js

  Modal.prototype.escape = function () { if (this.isShown && this.options.keyboard) { // The event listener is setup on initalization this.$element.on('keydown.dismiss.bs.modal', $.proxy(function (e) { e.which == 27 && this.hide() // !!! Does not check the instance options.keyboard flag status, so I had to add && this.options.keyboard here }, this)) } else if (!this.isShown) { this.$element.off('keydown.dismiss.bs.modal') } } 

And as I wrote in the code comment above adding the instance options.keyboard check in the event listener solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM