简体   繁体   English

在第一次Ajax调用之后,DOM在第二个依赖于第一个的Ajax调用之前没有更新

[英]After first Ajax call, DOM isn't updating before the second ajax call that's dependent on the first

I'm creating an app that allows people to drag from a List to a Slot and then the Slot can accept several sub-items in it, like it's Slot A-1, A-2,... 我正在创建一个应用程序,允许人们从列表拖动到插槽,然后该插槽可以在其中接受多个子项目,例如插槽A-1,A-2,...

All of this interaction is done through jQuery UI's draggable and droppable , but in the end, the function is called which contains an Ajax call to load the content for the Slot and it's sub items. 所有这些交互都是通过jQuery UI的draggabledroppable ,但是最后,该函数被调用,其中包含一个Ajax调用,以加载Slot及其子项的内容。

It's important to note that all sub-items are dependent on the Slot. 重要的是要注意,所有子项目都取决于广告位。

As I know the input I've tried simulating this on $(document).ready() and since ajax and the functions are asynchronous by default some are done faster, some slower and some sub items fail because the Slot isn't updated at that point. 据我所知,我已经尝试在$(document).ready()上对此进行仿真,并且由于默认情况下ajax和这些函数是异步的,因此某些操作速度更快,更慢,并且某些子项失败,因为Slot未在以下位置更新那一点。

I've tried to work around this by making those function calls async and awaiting the functions and now it's not working at all even though the calls are made in the correct order which is really confusing. 我试图通过使那些函数调用async并等待这些函数来解决此问题,即使调用以正确的顺序进行,这也根本不起作用,这确实令人困惑。

The dependency of the second ajax call on the first one is related to the first element having a specific data attribute that's required in the second call and for some reason, it's always undefined, even though it technically shouldn't be. 第二个ajax调用对第一个的依赖关系与第一个调用具有第一个调用所要求的具有特定数据属性的第一个元素有关,由于某种原因,尽管从技术上讲不应如此,但始终是未定义的。 I'll explain this further in the example below: 我将在以下示例中对此进行进一步说明:

I use this kind of setup (It's very simplified, but it should get the point across): 我使用这种设置(它非常简化,但是应该可以理解):

$(document).ready(function(){
    LoadDemo();
    //...
}); 

async function LoadDemo()
{
    await LoadSlots();
    await LoadSubitems();
}

async function LoadSlots()
{
    LoadSlot(x,y,z);
    LoadSlot(x,y,z);
    LoadSlot(x,y,z);
}
async function LoadSubitems()
{
    LoadSubitem(x,y,z,$(".Slot"))
    LoadSubitem(x,y,z,$(".Slot"))
    LoadSubitem(x,y,z,$(".Slot"))
}

Now inside the LoadSlot ajax calls a PHP script which generates HTML that gets inserted in the slot. 现在,在LoadSlot ajax内部调用一个PHP脚本,该脚本生成将HTML插入插槽中的HTML脚本。 this HTML also contains the required data attributes. 该HTML还包含必需的数据属性。 To be 100% sure the data information is loaded in jQuery as well (since I've heard there can be some issues with that for some reason) I manually update the data in the LoadSlot() call like this: 为了100%确保将数据信息也加载到jQuery中(因为我听说由于某些原因可能会出现一些问题),我在LoadSlot()调用中手动更新了数据,如下所示:

$(name).data("name",$(name).attr("data-name"));

I've traced the debug and can confirm that when this is called $(name).attr("data-name" is not undefined/null and has the proper value, which means after this, the data should also, however not until all javascript is finished processing, the data remains undefined. 我已经跟踪了调试,并可以确认当调用$(name).attr("data-name"未定义/为空并且具有正确的值,这意味着在此之后,数据也应该,但是直到所有javascript都已完成处理,数据仍未定义。

Additionally, even though the functions for adding SubItems appear AFTER the Slot calls, once the ajax error Alert shows up, the DOM still doesn't contain the HTML it should at that point, like it's stuck until all code is processed, even though it shouldn't be. 此外,即使添加了SubItems的功能出现在Slot调用之后,但一旦出现Ajax错误警报,DOM仍不包含该点应包含的HTML,就像它被卡住直到处理完所有代码一样,即使它不应该。

Once the initial javascript is over and I close all alerts, I can then do the drag/drop from the List (which, again, does the same thing, calls LoadSlot() or LoadSubitem() and it works just fine. 一旦初始javascript结束并且我关闭了所有警报,我就可以从列表中进行拖放(再次执行相同的操作,调用LoadSlot()LoadSubitem()即可正常工作。

So the question is basically this: How can I ensure that the DOM is properly updated with information from the LoadSlots() before LoadSubitems() is called? 所以,问题基本上是这样:我怎样才能确保DOM与信息正确更新从LoadSlots()之前LoadSubitems()被调用?

The problem is that you're not using await() in LoadSlots() , so it's not waiting for the AJAX calls to complete. 问题是您没有在LoadSlots()使用await() ,所以它没有在等待AJAX​​调用完成。

async function LoadSlots()
{
    await LoadSlot(x,y,z);
    await LoadSlot(x,y,z);
    await LoadSlot(x,y,z);
}

Of course, this means that you have to code LoadSlot() as a proper asynchronous function. 当然,这意味着您必须将LoadSlot()编码为适当的异步函数。 If it calls $.ajax() , you can simply have it return that call, since it returns a promise. 如果它调用$.ajax() ,则可以使它返回该调用,因为它返回了一个Promise。

If you don't want each of the LoadSlot() calls to wait for the previous one to finish, you can use Promise.all() : 如果您不希望每个LoadSlot()调用都等待上一个调用完成,则可以使用Promise.all()

async function LoadSlots() {
    await Promise.all([LoadSlot(x, y, z), LoadSlot(x, y, z), LoadSlot(x, y, z)]);
}

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

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