Ajax 注释插入 LI 还是替换 UL?

发布于 2024-11-01 20:45:59 字数 228 浏览 6 评论 0原文

我正在使用我的评论功能,并且正在两种策略之间进行选择。我的评论列表是一个无序列表。

  1. 将 DOM 中的 UL 替换为响应中的 UL。
  2. 将响应中的最后一个 LI 插入到 DOM 中的 UL 中。

第一个选项更简单,但我觉得这不是最好或正确的方法。 如果我想使用第二个选项,如果同时发布了多个评论,我如何知道响应中的哪些评论比 DOM 中的评论新?

想法?

I'm ajaxifying my comments feature and I'm choosing between 2 strategies. My comments list is an unordered list.

  1. Replace UL in DOM with UL from response.
  2. Insert last LI from response into UL in DOM.

The first option is simpler but I feel like it's not the best or right way.
If I want to use the second option how do I know which comments in the response are newer than the ones in the DOM if more than one comment was posted at the same time?

Thoughts?

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

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

发布评论

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

评论(4

请持续率性 2024-11-08 20:45:59

您可以将时间戳保留为变量以供 JS 引用。

当您加载页面时,您的服务器端语言会将要加载的最后一条评论的时间戳分配给 JavaScript 变量。从那时起,每次您加载更多评论时,请发送您请求的最后一个时间戳。您的服务器端语言将检查比该时间戳更新的评论。确保在发送更多请求时更新时间戳。通过将其作为响应的一部分发送或跟踪发送请求的时间。

You can keep the timestamp as a variable for JS to refer.

When you load the page, your server side language will assign the timestamp of the last comment to be loaded to a javascript variable. From there on, everytime you load more comments, send the last timestamp you requested. Your server side language will check for comments that are newer than that timestamp. Make sure to update the timestamp as you send out more requests. Either by sending it as part of the response or keeping track of when you sent the request.

花辞树 2024-11-08 20:45:59

我会选择选项 1。它实施起来要简单得多,而且实际上执行速度可能更快。但请确保没有任何事件处理程序绑定到 LI - 它们将在之后停止工作您更新其父 UL。使用委托来避免这种情况。

I would go for option 1. It is a lot simpler to implement and may actually perform faster. Make sure however do don't have any event handlers bound to the LIs - they will stop to work after you update their parent UL. Use delegates to avoid that.

七度光 2024-11-08 20:45:59

假设你的无序列表是这样的:

<ul id="commentsList">
<li>Comment 1</li>
</ul>

在 javascript 中你会写:

$.ajax({
  url: "the ajax url",
  type: "GET",
  data: {//whatever data},
  success: function(r){
    //Assuming r contains the comment. 
    //(Don't send html as the response but just the comment text
    $("#commentsList").append("<li>" + r + "<li>");
  }
});

Assuming your unordered list is like this:

<ul id="commentsList">
<li>Comment 1</li>
</ul>

in javascript you'll write:

$.ajax({
  url: "the ajax url",
  type: "GET",
  data: {//whatever data},
  success: function(r){
    //Assuming r contains the comment. 
    //(Don't send html as the response but just the comment text
    $("#commentsList").append("<li>" + r + "<li>");
  }
});
美男兮 2024-11-08 20:45:59

您应该在评论的 UL 后面附加最新的 LI为什么?您不必获取所有完整的 UL 列表并替换旧的列表。减少必须进行的更改。只需获取最新的 LI 并将其附加到最后即可。 更少的工作更高效

到目前为止,您的问题是关于如何知道哪一条是最新评论。当评论存储在数据库中时存储时间戳。在第一次获取评论的同时,尝试分配保存最后一项的时间戳的变量,然后在数据库中搜索大于您存储的时间戳的所有内容后,获取列表并附加它们,然后更新变量,再次使用最新的时间戳。

You should append the comment's UL with latest LIs. Why? you do not have to grab all the full UL list and replace the old one. Reduce the changes that has to be made. Just grab the latest LI and append it at the last. LESS WORK MORE EFFICIENT

So far of your question, about how to know which one is the latest comment. Store timestamp as the comments are stored in the DB. While also fetching the comments for the first time, try to assign the variable holding the timestamp of the last item, then after search the database for everything that is greating the timestamp you stored, get the list and append them, then update the variable, with the latest time stamp again.

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