Meteor's Iron Router doesn't close modal dialog

蹲街弑〆低调 提交于 2019-12-06 03:47:40

问题


I'm using Meteor and Iron Router, and I have a modal dialog that won't hide the backdrop when it gets dismissed. To be more accurate, I want that after clicking the dismiss button, the iron router will redirect to another page. The redirection code does work, but the backdrop stays visible. If I remove the routing line - the modal is dismissed and so does the backdrop.

Here is the modal's markup:

    <div class="modal fade" id="confirm-modal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title" id="modal-title">Are you sure?</h4>
            </div>
            <div class="modal-body">
                This cannot be undone.
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal" id="confirm-yes-button">Yes</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
            </div>
        </div>
    </div>
</div>

Here is the button that toggles the modal dialog:

<button type="button" id="delete-recipe-btn" data-target="#confirm-modal" data-toggle="modal" class="btn btn-primary btn-danger pull-right">Delete</button>

Here is the click event on the 'yes' button of the confirm modal dialog:

    'click #confirm-yes-button': function() {
    Recipes.remove(this._id);
    $('#confirm-modal').modal('hide');
    Router.go('/');
}

Why would the routing leave the backdrop visible?


回答1:


There are multiple solutions to this, depending on exactly how you desire the behavior. If you want the modal to hide first, then change the page, you can use a callback on the modal's behavior.

'click #confirm-yes-button': function() {
    Recipes.remove(this._id);
    $('#confirm-modal')
        .on('hidden.bs.modal', function() {
            Router.go('/');
        })
        .modal('hide');
}

As to your question of why the backdrop is visible - its complicated. The backdrop is only hidden once the "hide" animation completes, and changing the page interrupts/stops this behavior.




回答2:


One of the solution would be to use jQuery methods to remove backdrop once the user has been redirected.

Accounts.onLogin(function(){
    Router.go('/');
    $('.modal-backdrop').remove();
});

However to use this method you need have access to Accounts.login method which can be acquired by adding

gwendall:auth-client-callbacks

package to your meteor project



来源:https://stackoverflow.com/questions/24363375/meteors-iron-router-doesnt-close-modal-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!