Search code examples
jquerysimplemodal

After preventing onClose with SimpleModal, default close control doesn't work


In a basic SimpleModal implementation, I use the onClose option to check for a dirty form state and prevent closing:

.. 
    function onModalClose() {
       if (this.dirty && 
             !confirm('You have unsaved changes, continue anyway?')) {
           return;
       } 
       this.dirty=false;
       $.modal.close();
    }
..

Problem is if the user cancels the close operation, the default Close control on the dialog no longer works. $.modal.close() still works.

I am sure I can get around this by not using the default button, or doing something like actively re-binding my own close function to it, but this seems strange, and I wonder if there is some easy solution I'm overlooking.


Solution

  • It's a bit of a hack but I had success with the following code. (Note: I defined the onClose function, so this points to the modal dialog.)

     onClose: function () {
         if (this.dirty &&
                !confirm('You have unsaved changes, continue anyway?')) {
             this.bindEvents();
             this.occb = false;
             return;
         }
         $.modal.close();
      }