Skip to content

Instantly share code, notes, and snippets.

@martinnormark
Created January 6, 2013 10:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinnormark/4466467 to your computer and use it in GitHub Desktop.
Save martinnormark/4466467 to your computer and use it in GitHub Desktop.
Backbone implementation of the Twitter Bootstrap Modal view
(function () {
App.Views.ModalView = Backbone.View.extend({
tagName: "div",
template: modalTemplate,
initialize: function (options) {
_.bindAll(this, "render", "show", "hide", "toggle", "setContent");
if (options && options.header) {
this.Header = options.header;
}
this.ModalOptions = {};
if (options && options.preventAutoDismiss) {
this.ModalOptions["backdrop"] = "static";
this.ModalOptions["keyboard"] = false;
}
},
render: function (show) {
this.$el.html(modalTemplate({ Header: this.Header }));
this.ModalOptions["show"] = show !== false;
this.$el.modal(this.ModalOptions);
this.$body = this.$("div.modal-body");
return this;
},
show: function () {
this.$el.modal("show");
},
hide: function () {
this.$el.modal("hide");
},
toggle: function () {
this.$el.modal("toggle");
},
setContent: function (el) {
this.$body.empty().append(el);
},
remove: function () {
this.hide();
},
destroy: function () {
if (this.$el.data() && this.$el.data().modal) {
var modal = this.$el.data().modal;
if (modal.$backdrop) {
modal.$backdrop.remove();
}
if (modal.$element) {
modal.$element.remove();
}
}
$(".modal-open").removeClass("modal-open");
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment