Joshua Barker
Joshua Barker

Reputation: 1007

Bootstrap 2.3.1 popover causing parent modal to close

I'm running into an issue where I'm displaying some data in a Bootstrap modal. This data contains an icon which I'm turning into a popover. When I hover over the icon, the popover displays and everything works correctly, but when I mouse away from the icon, not only does the popover close, but the parent modal closes also.

I think this is the same issue as described here. However, the posted solution does not work for me. I'm capturing the popover's "hidden" event, but neither setting e.cancelBubble = true or calling e.stopPropagation() stops the parent modal from closing.

I don't have my code in front of me at the moment, but here is a rough mockup based on my general recollection...

HTML


<!-- ko with: myFoo -->
<div class="modal hide fade" data-bind="visible: isOpen">
    <div class="modal-header">
        <button type="button" class="close" data-bind="click: close">&times;</button>
        <h3>Title Bar!</h3>
    </div>
    <div class="modal-body">
        <!-- dynamically generated modal content goes here, including... -->
        <table>
            <tr>
                <td data-bind="popover: $data">
                    <i class="icon-question-mark" data-content="la la la..." />
                </td>
            </tr>
        </table>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn" data-bind="click: close">Close</a>
    </div>
</div>
<!-- /ko -->

Knockout Custom Binding Handler


ko.bindingHandlers.popover = {

    init: function(element)
    {
        $(element).children().andSelf().on("mousenter", "[data-content]"function() {
            var options = {...}
            $(this).popover(options).on("hidden", function(e) {
                e.cancelBubble = true;
                e.stopPropagation();
            });
        });
    }
};

Does anyone have any ideas / suggestions on how to fix this?

Upvotes: 5

Views: 2464

Answers (3)

DrewB
DrewB

Reputation: 3341

Credit goes to afanasy who mentioned this as a comment:

$("[data-toggle=popover]").on("hidden", function (e) {
   e.stopPropagation();
});

note: 'hidden' instead of 'hide'. This is for bootstrap 2.3.2.

Upvotes: 3

Paweł Staniec
Paweł Staniec

Reputation: 3181

$("[data-toggle=popover]").on("hide", function (e) {
    e.stopPropagation();
});

solved the issue for me

Upvotes: 2

Joshua Barker
Joshua Barker

Reputation: 1007

turns out that I needed to capture the on "hide" event instead of on "hidden", as the latter occurs AFTER the modal is already closed... see below for final solution:

Knockout Custom Binding Handler


define ["jquery", "ko", "bootstrap"], ($, ko) ->

    ko.bindingHandlers.popover =

        init: (element) ->

            $(element).children().andSelf().filter("[data-content]").popover()

            $(element).children().andSelf().on "hide", "[data-content]", (e) ->
                e.stopPropagation?()
                return

Upvotes: 0

Related Questions