htmx.ajax 在进行多个查询时仅调用两次

发布于 2025-01-14 16:24:29 字数 1255 浏览 0 评论 0原文

我在多个 x-data 块内的给定属性上使用 Alpinejs 的 $watch() ,以便在该属性发生更改时使用自定义处理程序调用 htmx.ajax 。下面是一个片段:

HTML:

<div x-data="{profiles:[]}">
  <div x-data="..." x-init="$watch('profiles', val=>onProfilesChanged())">...</div>

  <div x-data="..." x-init="$watch('profiles', val=>onProfilesChanged())">...</div>

  <div x-data="..." x-init="$watch('profiles', val=>onProfilesChanged())">...</div>

  <div>
  <!-- profiles modified here in response to user input -->
  ...
  </div>
</div>

Javascript:

function onProfilesChanged() {
  htmx.ajax('GET', ..., {'handler': (evt,response)=>{...}});
}

在浏览器中调试 我可以看到 onProfilesChanged 按预期被调用(3 次),但只发送了 2 个网络请求,并且回调处理程序只执行了两次。 这些请求似乎对应于第一次和最后一次调用,并且中间的所有内容都以某种方式丢失(尝试使用更多 $watch() 回调)。 Firefox 和 Chromium 上的行为是相同的,因此我怀疑这是浏览器限制,但我在 htmx 文档中找不到有关可排队的 Ajax 请求最大数量的任何内容。

我在这里完全不知所措,任何对可能发生的事情的了解将不胜感激。谢谢!

编辑:在 for 循环中使用 htmx.ajax() 时也会出现问题,所以这似乎确实是在 htmx 方面。有一个 hx-sync 标签来设置 hx-get/post.. 标签的队列行为,但似乎不适用于 JS API。

I am using Alpinejs' $watch() on a given property inside multiple x-data blocks to call htmx.ajax with a custom handler whenever that property changes. Here is a snippet:

HTML:

<div x-data="{profiles:[]}">
  <div x-data="..." x-init="$watch('profiles', val=>onProfilesChanged())">...</div>

  <div x-data="..." x-init="$watch('profiles', val=>onProfilesChanged())">...</div>

  <div x-data="..." x-init="$watch('profiles', val=>onProfilesChanged())">...</div>

  <div>
  <!-- profiles modified here in response to user input -->
  ...
  </div>
</div>

Javascript:

function onProfilesChanged() {
  htmx.ajax('GET', ..., {'handler': (evt,response)=>{...}});
}

Debugging in the browser I can see onProfilesChanged being called as expected (3 times), but only 2 network requests are sent and the callback handler is then only executed twice .
The requests seem to correspond to the first and last call, and everything in-between is somehow lost (tried with more $watch() callbacks). The behavior is the same on both Firefox and Chromium so I doubt it is a browser limitation, but I couldn't find anything in the htmx documentation concerning the maximum number of Ajax requests that can be queued.

I am at a total loss here, any insight into what might be going on would be greatly appreciated. Thanks!

Edit: the problem also happens when using htmx.ajax() inside a for loop, so this really does seem to be on the htmx side. There is an hx-sync tag to set the queue behavior for hx-get/post.. tags, but doesn't seem to work with the JS API.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2025-01-21 16:24:29

最后设法找到了解决方法:向 Ajax 请求的选项属性添加动态 source 元素修复了该问题。在 for 循环的情况下,我仍然限制每个元素 2 个请求,但来自不同来源的请求现在有不同的队列。

保留此信息以供将来参考,以防对其他人有所帮助。

Finally managed to find a workaround: adding a dynamic source element to the Ajax request's options attributes fixes the issue. I am still having a limit of 2 requests per element in the case of for loops, but requests from different sources have different queues now.

Keeping this for future reference in case it helps someone else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文