powmedia / backbone.bootstrap-modal

Bootstrap Modal wrapper for use with Backbone.
MIT License
183 stars 97 forks source link

Backbone.BootstrapModal

Usage

var view = new Backbone.View({...});

var modal = new Backbone.BootstrapModal({ content: view }).open();

Events

cancel

The user dismissed the modal (e.g. pressed cancel or Esc etc.)

ok

The user clicked OK

shown

Fired when the modal has finished animating in

hidden

Fired when the modal has finished animating out

Events in the view

You can listen to the events triggered by the modal inside the Backbone.View

var MyView = Backbone.View.extend({
    initialize: function () {
        this.bind("ok", okClicked);
    },

    okClicked: function (modal) {
        alert("Ok was clicked");
        modal.preventClose();
    }
});

var view = new MyView();

var modal = new Backbone.BootstrapModal({ content: view }).open();

Methods

new Backbone.BootstrapModal(options)

Set up the modal with the following options:

modal.open([cb])

Renders and opens the modal, running the optional callback if the 'OK' button is pressed

modal.close()

Close the modal and remove it from the DOM

modal.preventClose()

Prevents the modal from closing. Can be called from within a 'ok' or 'cancel' event listener:

var modal = new Backbone.BootstrapModal().open();

modal.on('ok', function() {
  //Do some validation etc.
  if (!isValid) modal.preventClose();
});

Live demo

You can read a short article and see live demo on sys.exit() blog.