Skip to content

Instantly share code, notes, and snippets.

@andrewshatnyy
Created July 31, 2013 22:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save andrewshatnyy/6126879 to your computer and use it in GitHub Desktop.
Save andrewshatnyy/6126879 to your computer and use it in GitHub Desktop.
Basic Bootstrap Modal in backbone that will clean up after itself to avoid memory leaks. View triggered on button click => modalView = new BaseModalView(); modalView.show();
<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">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<a href="#" class="btn">Close</a>
<a href="#" class="btn btn-primary">Save changes</a>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
define(['underscore', 'backbone', ], function(_, Backbone) {
var BaseModalView = Backbone.View.extend({
id: 'base-modal',
className: 'modal fade hide',
template: 'modals/BaseModal',
events: {
'hidden': 'teardown'
},
initialize: function() {
_(this).bindAll();
this.render();
},
show: function() {
this.$el.modal('show');
},
teardown: function() {
this.$el.data('modal', null);
this.remove();
},
render: function() {
this.getTemplate(this.template, this.renderView);
return this;
},
renderView: function(template) {
this.$el.html(template());
this.$el.modal({show:false}); // dont show modal on instantiation
}
});
return BaseModalView;
});
@yairEO
Copy link

yairEO commented Mar 26, 2014

Why don't you use removeData() instead of NULL-ing it?

@the1mills
Copy link

ummm, this.getTemplate() is not defined. There appear to be several problems with this example. Prove me wrong or take it down.

@eightyeight
Copy link

same issue, getTemplate() is a marionette.view method, so you need to use marionette over backbone if you want to use this realisation.

@andrewshatnyy
Copy link
Author

@yairEO hey, sorry haven't seen these comments. But it's better late then never. I had no idea removeData() existed then. Will try it on the next contract, thanks.

@andrewshatnyy
Copy link
Author

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