一个简单的引导模式 -
我们可以扩展此视图以向其添加更多功能。我们也可以传递数据以查看 init。
查看文件
/**
* ModalView
* @version 0.0.1
*/
var ModalView = Backbone.View.extend({
tpl: JST['modal-tpl'],
className: 'modal-view',
events: {
'evt:show': 'show',
'evt:hide': 'hide',
},
initialize: function(options) {
this.options = options;
_.bindAll(this, 'render', 'show', 'hide', 'remove');
this.render();
},
render: function() {
this.$el.html(this.tpl(this.options));
this.$el.appendTo('body');
this.$modal = this.$('.modal');
this.$modal.on('hidden.bs.modal', _.bind(function() {
this.close();
}, this));
},
show: function() {
this.$modal.modal('show');
},
hide: function() {
this.$modal.modal('hide');
},
close: function() {
//unbind events
this.unbind();
//Remove html from page
this.remove();
},
});
打开模态 -
//Pass modal data on init
var modal = new ModalView({
modalContent: 'Hello User!'
});
modal.show();
//modal.trigger('evt:show');
v3 的车把模板 -
<!-- Modal -->
<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="modal-label">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="modal-label">Modal</h4>
</div>
<div class="modal-body">
{{{modalContent}}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
- 在模态隐藏 - 我们必须删除模态 html,事件以避免多个实例。
- Bootstrap 模态 HTML 应附加到正文 - 如果不是,您可能会遇到 z-index/backdrop 问题。