Search code examples
javascriptjquerybackbone.jsmodal-dialog

How to use the cancelEl in a Backbone Modal?


I'm currently using this plugin for a modal: http://awkward.github.io/backbone.modal/

It comes with a cancelEl method to close the modal, and I am using it like so:

  var ActionModal = Backbone.Modal.extend({
    template: '#actionable-modal-template',
    cancelEl: '.bbm-btn-close'
  });

And here is what I am trying to do, written in pseudo code:

if (cancelEl.invoked) {
  // Do something
}

The psuedo code is based on the assumption that cancelEl causes clicking the backdrop of the modal, as well as the button, to close the modal.

How can I use the cancelEl method?

Thanks.


Solution

  • You can implement beforeCancel or cancel

      var ActionModal = Backbone.Modal.extend({
        template: '#actionable-modal-template',
        cancelEl: '.bbm-btn-close',
        beforeCancel: function() {
           // stuff
        }
      });
    

    Looking at the source code, beforeCancel can be used to cancel the modal close:

    if (this.beforeCancel) {
      if (this.beforeCancel() === false) {
        return;
      }
    }
    

    And cancel can be used to implement more functionality:

    if (typeof this.cancel === "function") {
      this.cancel();
    }