微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – 使用主干渲染引导模态

我认为一个代码会更好地解释我的问题:
风景:
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">&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 -->

父视图
//按钮点击以下被触发

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

// Modal view automatically bound to the body and removes itself on hidden;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐