The avid Impromptu enthusiast will be much pleased with the advancement of using events. Not only does 4.0 bring more events, it also brings better events. First and foremost I must point out there is a change to the state’s submit parameters. The first parameter is now an jQuery.Event, followed by the original 3 parameters from older versions. For backwards compatibility you can find on Github a tag for 3.3.3 which was committed with the new events, but with backwards compatibility of keeping the original parameters for the classic style of passing the submit option during the $.prompt call.

Ok, so why the change?

  1. So far with Impromptu you had to return true/false inside the submit callback to tell Impromptu whether or not to close the prompt. You can still do this, however the more appropriate way is to use the first parameter to call event.preventDefault() to prevent the prompt from closing. Think of it as how you would treat an anchor tag’s click event.
  2. You can call event.stopPropagation() to prevent event bubbling.
  3. You can now bind as many events as you like, though only one may be passed in the $.prompt() options.
  4. More events available through the same interface.

In addition to the change of how events work, there were additional events. Here are all available events:

  • promptloaded: called when the prompt has been displayed. Parameters (event)
  • promptsubmit: called on button click. Parameters (event, value, message, formVals)
  • promptclose: same as callback option. Called when prompt has closed. Parameters (event[, value, message, formVals]). Optional parameters are only available when a button was clicked.
  • promptstatechanging: called before state change. return false or event.preventDefault() stops the state change. Parameters (event, fromStateName, toStateName)
  • promptstatechanged: called after the state change is complete. Parameters (event, stateName)

Now you want to see what the difference is. Lets look at a quick example. I must mention you can still call $.prompt just like before, but just consider these changes “Extras”.


var jqi = $.prompt('Hello World!');

jqi.bind('promptsubmit', function(event, val, msg, fields){
    // To hold the prompt open you can either:
    // event.preventDefault()
    // or
    // return false;
});

Pretty straight forward. Again you can still use the submit, loaded, and callback options just like before with the exception of adding the event parameter first. Feel free to share your opinions!