使用Backbone启动Bootstrap模式

时间:2015-01-02 06:56:46

标签: twitter-bootstrap backbone.js modal-dialog

使用Backbone启动Bootstrap模式已被证明是一个巨大的痛苦。

以下是我的HTML文件中的内容:

<body>
<div id="teamSnapImportDiv"></div>
<script>

    function importFromTeamSnap(event) {
        event.preventDefault();
        alert('agegage');
        var aimportTeamSnapView = new importTeamSnapView({el: $("#teamSnapImportDiv")});
        var rendered = aimportTeamSnapView.render();
        //$(rendered.el).appendTo(this.$el).hide().fadeIn().slideDown();
    };

</script>


<script type="text/html" id="teamsnap-import-template">

        <div class="modal fade">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">Modal title</h4>
                    </div>
                    <div class="modal-body">
                        <p>One fine body&hellip;</p>
                    </div>
                    <div class="modal-footer">
                        <button id="cancelTeamSnapImport" type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button id="confirmTeamSnapImport" type="button" class="btn btn-primary">Save changes</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->


</script>
</body>

这是我的Backbone视图:

var importTeamSnapView = Backbone.View.extend({
    tagName: "div",
    id: 'teamSnapImportDiv',
    initialize: function() {
    },
    render: function (){
        var importTeamSnapTemplate = document.getElementById('teamsnap-import-template').innerHTML;
        this.$el.html(_.template(importTeamSnapTemplate)());
    }
});

我需要做些什么来完成这项工作?我可以得到其他的东西,但模态的东西是行不通的。其他示例并未说明需要完成的工作或原因。

2 个答案:

答案 0 :(得分:0)

这可以很好地解决问题。

http://awkward.github.io/backbone.modal/

不幸的是,这是另一个图书馆,但它确实有用。

答案 1 :(得分:-1)

尝试使用this approach

模态JS

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
    }
 });

把手模板

<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;