I am using Twitter Bootstrap modals, with the default options where you can click the backdrop or press [Esc] to close the modal.
However, when I initiate an ajax operation in the modal I want to disable the modal from being closed in any way. So I disable buttons and hide the modal's close button but I can't figure out how to disable the backdrop and the [Esc] key.
I tried:
$('#myModal').modal({
backdrop: 'static',
keyboard: false
});
But this doesn't seem to work on the fly.
I will also need to re-enable the backdrop and keyboard once the ajax operation is finished.
You are not the only one who are missing that feature. I think bootstrap sometimes is too "minimalistic", the people behind has the idea a lot should be done in the "implementation layer", but it is to no use when the bootstrap jQuery plugins themselves makes it impossible!
You have to implement the functionality yourself, like this :
in
bootstrap.js
v2.1.1 modal begins at line 61.in
Modal.prototype
, add two functions,lock
andunlock
, so it looks like this (I show here only the beginning ofmodal.prototype
, becuase it is too much code)Then, also in Modal.prototype, find the function
hide
, and add a line so it looks like this (again, only top of hide is showed)And finally, alter
$.fn.modal.defaults
to :Now you have on-the-fly lock/unlock functionality in your bootstrap modal, preventing the user from closing the modal at critical moments.
Example :
This is an altered version of "Live Demo" from http://twitter.github.com/bootstrap/javascript.html#modals
I have inserted two buttons, "lock" and "unlock" - when clicked, they set the modal in either locked or normal mode (the settings it is initialised with)
Edit, in your case, you just have to call lock/onlock when doing ajax :
You can create a variable isBlocked that you can set while doing AJAX calls to true, then you can check it on bootsrap modal hide event this way:
I think this way is easier than extending Bootsrap, hope it helps someone :D
Thanks @davidkonrad for your work on this. I was trying to implement this as well and it seems that things have changed with bootstrap 3. Now instead of:
_superModal.defaults
it is now attached to the constructor so you have to do
_superModal.Constructor.DEFAULTS
Also the constructor has changed which meant that I had to copy it and modify it which is less than ideal. Instead I've come up with the following code that works and does not copy the constructor and should be more fool-proof if bootstrap changes going forward. Have a go:
So in order to lock the modal:
$('#dlg').modal('lock');
and to unlock:
$('#dlg').modal('unlock');
Yay!
Note: This solution is targeting twitter bootstrap 2.x! See this answer (just below) for differences according to bootstrap 3.
Extending bootstrap modal functionality without modifying original source.
Thanks to @David and his suggestion at How to Extend Twitter Bootstrap Plugin I finally got it to work. It is a slightly modified version of his solution with modal "lock" added. I post it as a additional answer since I think it may could be a starting point for others that like me have struggled hard with this issue.
With this technique it should not be nessecary to alter bootstrap.js, and the same functionality can more easily be shared among bootstrap projects. This method should be applicable to all the other bootstrap plugins. Have so far only tried with button though, but I cant se why it shouldnt.
see working fiddle -> http://jsfiddle.net/Sz7ZS/
There is an easier way to do it. This bootstrap pull request explains a little more. The solution disables all methods for closing the modal (keyboard, mouse click, close button).
All you have to do to disable closing the modal is:
To enable closing again:
Example
Here is some sample code that works in conjunction with a jQuery get:
I updated this awesome script to work with Bootstrap 4 & 3. I'm keeping both scenarios because I have a global script that I'm using in my projects, and some of them are using both version of Boostrap (don't worry, one bootstrap per project). If anyone has a better idea, please share it with us.