Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Created April 11, 2012 14:37
Show Gist options
  • Save mxriverlynn/2359717 to your computer and use it in GitHub Desktop.
Save mxriverlynn/2359717 to your computer and use it in GitHub Desktop.
modal dialog with backbone
<script id="modal-view-template" type="text/html">
<div class="modal-header">
<h2>This is a modal!</h2>
</div>
<div class="modal-body">
<p>With some content in it!</p>
</div>
<div class="modal-footer">
<button class="btn">cancel</button>
<button class="btn-default">Ok</button>
</div>
</script>
<div id="modal"></div>
var view = new MyView();
view.render();
var $modalEl = $("#modal");
$modalEl.html(view.el);
$modalEl.modal();
var ModalRegion = Backbone.Marionette.Region.extend({
el: "#modal",
constructor: function(){
_.bindAll(this);
Backbone.Marionette.Region.prototype.constructor.apply(this, arguments);
this.on("view:show", this.showModal, this);
},
getEl: function(selector){
var $el = $(selector);
$el.on("hidden", this.close);
return $el;
},
showModal: function(view){
view.on("close", this.hideModal, this);
this.$el.modal('show');
},
hideModal: function(){
this.$el.modal('hide');
}
});
var App = new Backbone.Marionette.Application();
App.addRegions({
main: "#main-content",
modal: ModalRegion
});
// somewhere else in the app
var view = new MyView();
App.modal.show(view);
@ijdickinson
Copy link

I think line 5 of 3.js should be:

_.bindAll( this, "getEl", "showModal", "hideModal" );

otherwise, bindAll complains that it should be passed some function names.

@insaciableslabs
Copy link

I made a gist that worked for our current project with Bootstrap and Backbone 1.0 https://gist.github.com/insaciableslabs/6369107

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment