Skip to content

Instantly share code, notes, and snippets.

@jardix22
Forked from mxriverlynn/1.html
Last active March 6, 2017 16:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jardix22/7451746 to your computer and use it in GitHub Desktop.
Save jardix22/7451746 to your computer and use it in GitHub Desktop.
Modal Bootstrap 3 + Marionette.js
<script id="modal-view-template" type="text/html">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</script>
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment