0

as subject, I've already read another question regarding the similar problem: modal-dialog-with-backbone-and-marionette

But I don't want to introduce backbone.marionette into my project.

I'm wondering if there's any other alternatives?

Community
  • 1
  • 1
yuwang
  • 846
  • 11
  • 24

1 Answers1

3

I solved it by placing and empty div as container for modal dialog into the bottom of my page:

    <!-- Charge -->
    <div id="charge_dialog_container" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

    </div>

defined the rest of the dialog in the template:

    <script id="charge_dialog_template" type="text/template">
        <div class="modal-header">
            ...
        </div>
        <div class="modal-body">
            ...
        </div>
        <div class="modal-footer">
          <button class="btn btn-primary" data-dismiss="modal"><i class="icon-ok"></i> OK</button>
        </div>
    </script>

During the view construction in initialize function I did the modal() stuff:

        ChargeView = Backbone.View.extend({
            el: $('#charge_dialog_container'),
            initialize: function() {
                this.render();
                this.$el.modal({'backdrop': 'static'});
            },
            render: function() {
                var template = _.template($('#charge_dialog_template').html());
                this.$el.html(template);
            }
        });
Akos K
  • 7,071
  • 3
  • 33
  • 46
  • looks a nice approach, will try later and give feedback afterwards, thanks ;) – yuwang Feb 28 '13 at 10:33
  • awesome, applied and event bindings work like a charm, really nice ;) – yuwang Mar 01 '13 at 05:40
  • I had to unbind when closing the dialog (cancel, save etc) otherwise I found events were fired N times (where N was the number of times the dialog had been opened) for old modal dialogs - $(@el).unbind() did it. – RidingTheRails May 01 '13 at 09:22
  • or probably `$el.remove()`, event tends to be polluted with in-appropriate bindings. – yuwang May 23 '13 at 17:26