使用骨干渲染bootstrap模式

roy*_*oyB 9 jquery backbone.js twitter-bootstrap

我认为代码会更好地解释我的问题:视图:

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;

  }

});
Run Code Online (Sandbox Code Playgroud)

实例化时:

 var error = new App.Models.Errors({title: "Exporting Error", content: "Error"});
 var errorModal = new App.Views.ErrorModal({model: error});
 errorModal.render();
Run Code Online (Sandbox Code Playgroud)

模态已加载,但我只得到一个空div

谢谢您的帮助!罗伊

And*_*nyy 21

最好创建一个包含所有Modal逻辑的单独类,然后从主视图中调用它.

尝试使用这种方法.

莫代尔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
    }
 });
Run Code Online (Sandbox Code Playgroud)

把手模板

<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 -->
Run Code Online (Sandbox Code Playgroud)

父视图 //按钮点击后触发

modalView = new BaseModalView();
modalView.show();

// Modal view automatically bound to the body and removes itself on hidden;
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用Bootsrap 3,请确保您的视图侦听"hidden.bs.modal".伙计们用新版本的Bootsrap http://getbootstrap.com/javascript/#modals更改了活动的名称 (6认同)