简体   繁体   English

Semantic-ui模态复制

[英]Semantic-ui modal duplicating

I have a Semantic-ui modal on a route that initially works as expected (first time the app is loaded). 我在路线上有一个Semantic-ui模态,最初按预期工作(第一次加载应用程序)。 I can open and close multiple times without issue. 我可以多次打开和关闭,没有问题。 If I navigate away from the route and back again and then click the modal button again (which fires the event below) the modal html is duplicated. 如果我离开路线并再次返回,然后再次单击模式按钮(触发下面的事件),模式html将被复制。 Each time I do this (navigate away and back), another modal is added. 每次我这样做(导航和返回),都会添加另一个模态。

My modal is broken into two templates, which as I read, is the way it should be. 我的模态分为两个模板,正如我所读到的,它应该是它的样子。

html: HTML:

<template name="contentList">
  {{> addContent}}
  <button class="ui blue button" href="#" id="modal">
    Add Content
  </button>
</template>

<template name="addContent">
  <div id="modal" class="ui small addContent modal">
    {{>addContentForm}}
  </div>
</template>

<template name="addContentForm">
  <!-- Form HTML goes here -->
</template>

js: JS:

Template.contentList.events({
  'click #modal': function(e, t) {
    event.preventDefault();
    $('.ui.modal')
    .modal({
      onApprove : function() {
        $('.ui.modal').modal('hide');
      }
    })
    .modal('show');
  }
});

What do I need to do to stop this behaviour? 我该怎么做才能阻止这种行为?

Well don't I feel like a fool... 好吧,我不觉得自己像个傻瓜......

The reason the modal div was duplicating was because the {{>addContent}} modal was placed within a div. 模态div重复的原因是因为{{>addContent}}模式放在div中。 Moving that code out of a div fixed the issue. 将该代码移出div修复了该问题。 My fault for simplifying the code in my question too much! 我错误地简化了我的问题中的代码!

Update 更新

After testing some more with the workaround I had below, I encountered issues where buttons wouldn't respond. 在使用下面的解决方法测试了一些后,我遇到了按钮无法响应的问题。 After digging around further, I stumbled upon this bug report for Semantic-ui: https://github.com/Semantic-Org/Semantic-UI/issues/3200 . 在进一步挖掘之后,我偶然发现了Semantic-ui的这个错误报告: https//github.com/Semantic-Org/Semantic-UI/issues/3200

Even though the problem has been around since 2016, it appears to be still unresolved. 尽管问题自2016年以来一直存在,但似乎仍未解决。 Several solutions are offered here. 这里提供了几种解决方案。 The one that worked well for me is calling $('body .modals').remove(); 对我来说效果很好的是调用$('body .modals').remove(); in the onCreated function of my dynamic templates. 在我的动态模板的onCreated函数中。 You could also remove specific modals that give you trouble: $('body .modals .addItem') . 你也可以删除给你带来麻烦的特定模态: $('body .modals .addItem') This way, any old modals along with their event bindings are removed first before new ones are added. 这样,在添加新模态之前,首先删除任何旧模态及其事件绑定。 I'm still not entirely sure if this is exactly what's going on, but my testings seem to support it. 我仍然不完全确定这是不是正在发生的事情,但我的测试似乎支持它。

Original post 原帖

I am having a very similar issue. 我有一个非常类似的问题。 The marked answer does not solve it for me. 明确的答案并没有为我解决。 I have a div with the modal class. 我有一个模态类的div。 In it I load the content via template. 在其中我通过模板加载内容。 It displays two modals after opening, closing, navigating away and back, and open it again. 它在打开,关闭,导航和返回后显示两个模态,然后再次打开它。

I noticed the modal gets added twice to the dimmer div somewhere during navigation. 我注意到在导航过程中,模态会在某处添加两次到调光器div。 So I wrote a small function to prevent a modal from being added more than once. 所以我写了一个小函数来防止模态被多次添加。 Be aware that this is a workaround, and there may be a deeper problem going on with larger consequences. 请注意,这是一种解决方法,可能会出现更深层次的问题,后果更严重。

 Template.editor.helpers({ modalNotAdded: function(modalClass) { //search in .ui.modals div, an element of the dimmer, for modalClass (.addItemModal in this example). If not found, return true. return $(".ui.modals").find(modalClass).length == 0; } }); 
 <template name="editor"> <!-- only add modal if modalNotAdded helper function with argument ".addItem" returns true !--> {{#if modalNotAdded ".addItem"}} <div class="ui longer modal addItem"> {{> addItemModal}} </div> {{/if}} </template> <template name="addItemModal"> <!-- your modal content !--> </template> 

Having multiple dom elements with same id can result unexpected behaviour. 拥有多个具有相同id的dom元素可能会导致意外行为。 You have a div with id "modal" and a button again with id "modal". 你有一个id为“modal”的div和一个id为“modal”的按钮。 Change the id of button. 更改按钮的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM