当前位置: 首页 > 知识库问答 >
问题:

将$.get方法移动到它自己的函数中以避免重复

冯枫
2023-03-14

我有以下函数用于从php json_encode中提取数据以在FullCalendar中使用。

    eventDrop: function(info) {
    $.get( "php/get-events.php", function( data ) {
    // data is your result
    // Find the value for editable where the event id = the event you are trying to move 
    rawdata = JSON.parse(data);
    editable = rawdata.find(x => x.id === info.event.id).editable;
   start= info.event.start.toISOString();
   start = moment(info.event.start).format('Y-MM-DD HH:mm:ss');
   end= info.event.end.toISOString();
   end = moment(info.event.end).format('Y-MM-DD HH:mm:ss');
   title = info.event.title;
   id = info.event.id;
    });
   }

我将对fullcalendar中的eventResize函数使用非常类似的代码,因此我想提取这一部分

 $.get( "php/get-events.php", function( data ) {
    // data is your result
    // Find the value for editable where the event id = the event you are trying to move 
    rawdata = JSON.parse(data);

它自己的功能(不是100%确定我在这里使用了正确的术语?)关于如何在全局范围内传递变量,我看到了这个答案jQuery-将函数中的变量传递给另一个函数,所以我尝试将上面的代码从eventDrop中移出,如下所示

$.get( "php/get-events.php", function( data ) {
    // data is your result
    // Find the value for editable where the event id = the event you are trying to move 
    rawdata = JSON.parse(data);
});
   eventDrop: function(info) {

但这给了我一个错误

Uncaught SyntaxError: Unexpected token '.'

理想情况下,我希望在整个页面中使用$.get只执行一次json提取,然后引用rawdata全局变量来读取信息,这可能吗?

目前我的全部解决方案是

<script>
  document.addEventListener('DOMContentLoaded', function() {
    var calendarEl = document.getElementById('calendar');
    var today = moment().day();
    var calendar = new FullCalendar.Calendar(calendarEl, {
      headerToolbar: {
        left: 'prev,next today',
        center: 'title',
        right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
      },
      defaultDate: today,
      editable: true,
      
    $.get( "php/get-events.php", function( data ) {
    // data is your result
    // Find the value for editable where the event id = the event you are trying to move 
    rawdata = JSON.parse(data);
    });

    eventDrop: function(info) {
    editable = rawdata.find(x => x.id === info.event.id).editable;
   start= info.event.start.toISOString();
   start = moment(info.event.start).format('Y-MM-DD HH:mm:ss');
   end= info.event.end.toISOString();
   end = moment(info.event.end).format('Y-MM-DD HH:mm:ss');
   title = info.event.title;
   id = info.event.id;
   if (!confirm("Confirm you want to change " + info.event.title + " to " + info.event.start)) {
      info.revert();
    }
    else{
      if(editable === 'Y'){
        $.ajax({
        url: 'php/calendarupdate.php',
        data: 'title=' + info.event.title + '&start='+ start +'&end=' + end + '&id=' + info.event.id ,
        type: "POST"
        });
      } 
      else{
        alert("Can only modify this calendar event if you created it. Please ask the event creator to modify.");
        calendar.refetchEvents();
      }
    }

   
   },

      navLinks: true, // can click day/week names to navigate views
      dayMaxEvents: true, // allow "more" link when too many events
      events: {
        url: '/php/get-events.php',
        failure: function() {
          document.getElementById('script-warning').style.display = 'block'
        }
      },
      loading: function(bool) {
        document.getElementById('loading').style.display =
          bool ? 'block' : 'none';
      }
    });

    calendar.render();
  });

</script>

共有1个答案

闻华容
2023-03-14

问题解决了,多亏了@Patrick Evans的建议,我把get调用添加到了代码的中间,我不得不把它添加到最后,在“;”之后。结束这条线。我现在可以在EventDrop中引用“rawData”变量。

<script>
  document.addEventListener('DOMContentLoaded', function() {
    var calendarEl = document.getElementById('calendar');
    var today = moment().day();
    var calendar = new FullCalendar.Calendar(calendarEl, {
      headerToolbar: {
        left: 'prev,next today',
        center: 'title',
        right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
      },
      defaultDate: today,
      editable: true,
      
    eventDrop: function(info) {
    editable = rawdata.find(x => x.id === info.event.id).editable;
   start= info.event.start.toISOString();
   start = moment(info.event.start).format('Y-MM-DD HH:mm:ss');
   end= info.event.end.toISOString();
   end = moment(info.event.end).format('Y-MM-DD HH:mm:ss');
   title = info.event.title;
   id = info.event.id;
   if (!confirm("Confirm you want to change " + info.event.title + " to " + info.event.start)) {
      info.revert();
    }
    else{
      if(editable === 'Y'){
        $.ajax({
        url: 'php/calendarupdate.php',
        data: 'title=' + info.event.title + '&start='+ start +'&end=' + end + '&id=' + info.event.id ,
        type: "POST"
        });
      } 
      else{
        alert("Can only modify this calendar event if you created it. Please ask the event creator to modify.");
        calendar.refetchEvents();
      }
    } 
   },
      navLinks: true, // can click day/week names to navigate views
      dayMaxEvents: true, // allow "more" link when too many events
      events: {
        url: '/php/get-events.php',
        failure: function() {
          document.getElementById('script-warning').style.display = 'block'
        }
      },
      loading: function(bool) {
        document.getElementById('loading').style.display =
          bool ? 'block' : 'none';
      }
    });
    $.get( "php/get-events.php", function( data ) {
    // data is your result
    // Find the value for editable where the event id = the event you are trying to move 
    rawdata = JSON.parse(data);
    });

    calendar.render();
  });

</script>
 类似资料:
  • 问题 你在类中需要重复的定义一些执行相同逻辑的属性方法,比如进行类型检查,怎样去简化这些重复代码呢? 解决方案 考虑下一个简单的类,它的属性由属性方法包装: class Person: def __init__(self, name ,age): self.name = name self.age = age @property def n

  • 我们在遗留代码和当前代码之间有一个粘合组件。本质上,整个遗留应用程序是单线程的,并且存在可怕的问题,单个指令的用户界面刷新可能会发生5到8次。 我想在第一次更新请求发生2秒后发布一条异步消息。 让我们不要纠结于为什么,这不是我真正想要做的,但我必须了解如何至少做到这一点,这样我才能实施真正的解决方案。 理论是:如果未来的任务不存在,我们会创建它,一旦它存在,如果它没有完成(因为这是错误的遗留更新4

  • 本文向大家介绍Python自定义scrapy中间模块避免重复采集的方法,包括了Python自定义scrapy中间模块避免重复采集的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Python自定义scrapy中间模块避免重复采集的方法。分享给大家供大家参考。具体如下: 希望本文所述对大家的Python程序设计有所帮助。

  • 本文向大家介绍Shell脚本避免重复执行的方法,包括了Shell脚本避免重复执行的方法的使用技巧和注意事项,需要的朋友参考一下 很多用cron定时执行的shell脚本可能会由于各种原因执行很久,会有必要在运行的时候先检查一下自身是否还在运行。本文提供的linux shell脚本用以检查以命令sh ...来执行的shell脚本。要对其他东西进行唯一性检查,可以稍微修改一下源代码。

  • 问题内容: 我有一个包含3列的表格- id(pk),pageId(fk),名称。我有一个PHP脚本,它将大约5000条记录转储到表中,其中大约一半是重复的,具有相同的pageId和名称。pageId和名称的组合应该是唯一的。当我遍历php中的脚本时,防止重复项被保存到表中的最佳方法是什么? 问题答案: 第一步是在表上设置唯一键: 然后,当有重复项时,您必须决定要做什么。你应该: 忽略它? 覆盖先前

  • 我有一个项目,其中log4j到slf4j-2.17.1.jar作为依赖项的一部分自动下载。我还添加了排除标记。但仍然在下载它。 需要注意的是,我在pom.xml.的属性标签中指定了log4j2版本,它正在下载log4j-api-2.17.1.jar(这是预期的)log4j-to-slf4j-2.17.1.jar(这不是必需的)。 有没有解决这个问题的办法?请注意,我已经检查过,jar在类路径中也不