简体   繁体   English

Javascript + Drupal

[英]Javascript + Drupal

My setup: A "open video" button. 我的设置:“打开视频”按钮。 A form, when user clicks handouts or videos, ajax is called to render the files with the buttons. 表单,当用户单击讲义或视频时,将调用ajax以使用按钮呈现文件。

My question is: 我的问题是:

When I render the page and click on "open the video", I have to click out twice to exit. 当我渲染页面并单击“打开视频”时,我必须单击两次以退出。 I assume it is because ModalVideo is being called twice, once when it rendered and once when the form is submitted. 我认为这是因为ModalVideo被调用了两次,一次是在渲染时,一次是在提交表单时。 I was wondering if anyone has any pointers or references to drupal behaviors on forms/javascript being ran once. 我想知道是否有人对窗体/ javascript上的drupal行为有任何指针或引用,因此只能运行一次。 Thank you. 谢谢。

Drupal.behaviors.customJS = {
  attach: function(context, settings){
     // import ModalVideo from 'modal-video';
     new ModalVideo(".js-video-vimeo-btn",{channel:'vimeo'});
  }
}

------UPDATED SOLUTION------- ------更新的解决方案-------

 Drupal.behaviors.customJS = {
  attach: function(context, settings){
     //every time the view is updated on ajax call, run only once
     $('.view-pdf-views').once(function(){
         // import ModalVideo from 'modal-video';
        new ModalVideo(".js-video-vimeo-btn",{channel:'vimeo'});
     });
  }
 }

You have to use once() plugin that is delivered to Drupal. 您必须使用交付给Drupal的Once()插件。 Check JS API Overview 检查JS API概述

If you are using .on() to listen for button click you should change it to .one() 如果您使用.on()侦听按钮单击,则应将其更改为.one()

http://api.jquery.com/one/ http://api.jquery.com/one/

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

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