我认为一个代码会更好地解释我的问题:
风景:
风景:
App.Views.ErrorModal = Backbone.View.extend({ template: window.template('errorModal'),render: function(){ this.$el.html(this.template(this.model.toJSON())); this.$("#errorApproveModal").modal({ keyboard: true }); return this; } });
实例化时:
var error = new App.Models.Errors({title: "Exporting Error",content: "Error"}); var errorModal = new App.Views.ErrorModal({model: error}); errorModal.render();
模态被加载,但我只得到一个空的div
谢谢您的帮助!
罗伊
解决方法
总是最好创建一个单独的类来保存所有的Modal逻辑,然后从主视图中调用它.
尝试使用this approach.
模态JS
var BaseModalView = Backbone.View.extend({ id: 'base-modal',className: 'modal fade hide',template: 'modals/BaseModal',events: { 'hidden': 'teardown' },initialize: function() { _.bindAll(this,'show','teardown','render','renderView'); 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 } });
手把模板
<div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</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 -->
父视图
//按钮点击以下被触发
modalView = new BaseModalView(); modalView.show(); // Modal view automatically bound to the body and removes itself on hidden;