为什么引导日期选择器在显示时会触发"show.bs.modal"? [英] Why does bootstrap-datepicker trigger 'show.bs.modal' when it is displayed?

查看:145
本文介绍了为什么引导日期选择器在显示时会触发"show.bs.modal"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模态,如下所示,

I have a modal, illustrated below,

当我选择包含日期选择器的输入字段时,会触发引导程序模态事件.on('show.bs.modal'),这是一个超级问题,因为当模态事件发生时,我会采取各种异步操作合法显示.我认为该模态已经显示,并且该事件不应该触发.

When I select the input field that contains the date picker, the bootstrap modal event .on('show.bs.modal') gets triggered, which is super problematic because I'm taking all kinds of async action when the modal is legitimately shown. I'm of the opinion that the modal is already shown and this event should not be firing.

我在引导事件'show.bs.modal'上有一个侦听器,如下所述,

I have a listener on the bootstrap event 'show.bs.modal' as referenced below,

  handleModalLaunch: () ->
    $(@modalClass).on 'show.bs.modal', (event) =>
      return if event.dates
      promise = new Promise ( (resolve, reject) =>
        @setModalData(event)
        if (@interactionData)
          resolve(@interactionData)
        else
          reject(false)
      )
      promise.then(
        (results) =>
          @trigger 'setRooms', @callBacks
          @trigger 'setStudentInfo', @callBacks
        (err) ->
          err
      )

有效地,再次触发了侦听器,该侦听器随后又调用了promise和相关的回调,事件的触发是有问题的,因为当然,已经显示了模式并且我不希望运行这些回调/承诺.

And effectively, the listener is being triggered again which is subsequently calling the promise and associated callbacks, the triggering of the event is problematic because of course, the modal is already show and I don't want these callbacks/promise being run.

我添加了return if event.dates(event.dates是datepicker事件唯一的属性),以基本上在发生日期选择器触发模态显示事件的情况下使该代码短路,但是,当然,这很hacky并且我想更好地了解为什么datepicker本身会触发show事件.潜在地,由于我的连播监听器都与模式类绑定,因此显示datepicker的动作可能继承了父模式的目标,并且很可能本身就是一个模式,即,显示了modal(datepicker),并且由于datepicker继承自父模式,事件触发时就好像它是显示"的父模式一样.我完全混淆了吗? (实际上,现在对我来说似乎更清楚了,但仍然需要了解如何解决.)

I added return if event.dates (event.dates being a property unique to the datepicker event), to basically short circuit this code in the event that the date-picker triggered the modal show event, but of course, this is hacky and I'd like to better understand why the datepicker itself is triggering the show event. Potentially, since my show even listener is tied to the class of the modal, the act of showing the datepicker probably inherits the target of the parent modal and is likely itself a modal, ie, the modal(datepicker) is shown and since the datepicker inherits from the parent modal, the event triggers as though it was the parent modal being 'shown'. Have I utterly confused this? (Actually, it seems clearer to me now, but still need to understand how to fix.)

推荐答案

这是日期选择器库中的错误.您可以在此处上对其进行跟踪. @kroeder在那里提供了解决方法

This is a bug in date picker library. You can track it on github here. There is workaround given there by @kroeder

$("#my-datepicker").datepicker().on('show.bs.modal', function(event) {
    // prevent datepicker from firing bootstrap modal "show.bs.modal"
    event.stopPropagation(); 
});

这篇关于为什么引导日期选择器在显示时会触发"show.bs.modal"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆