简体   繁体   English

使用Ajax加载新内容

[英]Loading New Content with Ajax

This is a situation I've run into several times, not sure what the best approach is. 我已经遇到过几次这种情况,不确定最好的方法是什么。

Let's say I have some kind of dynamic content that users can add to via Ajax. 假设我有一些动态内容,用户可以通过Ajax添加到其中。 For example, take Stack Overflow's comments - people can add comments directly on to the page using Ajax. 例如,使用Stack Overflow的注释-人们可以使用Ajax将注释直接添加到页面上。

The question is, how do I implement the addition itself of the new content. 问题是,如何实现新内容的添加本身。 For example, it looks like the ajax call which Stack Overflow uses to add a comment, simply returns html which replaces all of the comments (not just the new one). 例如,看起来好像ajax调用(Stack Overflow用于添加注释),仅返回html即可替换所有注释(而不仅仅是新注释)。 This way saves redundant logic in the Javascript code which has to "know" what the html looks like. 这种方式将冗余逻辑保存在Javascript代码中,该逻辑必须“知道” HTML的外观。

Is this the best approach? 这是最好的方法吗? If so, how best to implement it in Django so that there is no redundancy (ie, so that the view doesn't have to know what the html looks like?) 如果是这样,如何最好地在Django中实现它,以确保没有冗余(即,视图不必知道html的外观?)

Thanks! 谢谢!

EDIT: In my specific case, there is no danger of other comments being added in the meantime - this is purely a question of the best implementation. 编辑:在我的特定情况下,没有其他评论同时被添加的危险-这纯粹是最佳实现的问题。

If the content is simple, I would get JSON and build the HTML in jQuery. 如果内容很简单,我将获取JSON并在jQuery中构建HTML。 If it's complex, I would create a template and call render() on it on the server and return the HTML (which jQuery could either append to other content or replace existing content). 如果很复杂,我将创建一个模板并在服务器上对其进行调用render()并返回HTML(jQuery可以附加到其他内容或替换现有内容)。

The reason to update all of the comments is to take in account other comments that other people may have also submitted in the meantime. 更新所有评论的原因是要考虑到其他人同时也可能提交的其他评论。 to keep the site truly dynamic, you can do either that, or even, when the page is loaded, load up a variable with the newest submitted comment ID, and set a timer that goes back to check if there are more comments since then. 要使网站真正保持动态,您可以执行此操作,甚至在页面加载时执行此操作,使用最新提交的评论ID加载变量,并设置一个计时器,此后返回以检查自那时以来是否还有更多评论。 if there are, return them as a JSON object and append them onto the current page a DIV at a time. 如果存在,则将它们作为JSON对象返回,并将它们一次DIV附加到当前页面上。 That would be my preferred way to handle it, because you can then target actions based on each DIV's id or rel, which directly relates back to the comment's ID in the database... 那将是我首选的处理方式,因为您随后可以基于每个DIV的ID或rel来定位操作,而这些ID或rel直接与数据库中注释的ID相关联...

I am not proficient neither with Django nor Python but I suppose the basic logic is similar for all server-side languages. 我既不精通Django,也不精通Python,但我想所有服务器端语言的基本逻辑都是相似的。

When weighing the pros and cons of either approach things depend on what you want to optimize. 在权衡两种方法的利弊时,事情取决于您要优化的内容。 If bandwidth is important then obviously using pure JSON in communication reduces latency when compared to transmitting ready-made HTML. 如果带宽很重要,那么与传输现成的HTML相比,显然在通信中使用纯JSON可以减少延迟。

However, duplicating the server-side view functionality to Javascript is a tedious and error-prone process. 但是,将服务器端视图功能复制到Javascript是一个乏味且容易出错的过程。 It simply takes a lot of time. 这只需要很多时间。

I personally feel that in most cases (for small and medium traffic sites) it's perfectly fine to put the HTML fragment together on server side and simply replace the contents of a container (eg contents of a div ) inside an AJAX callback function. 我个人认为,在大多数情况下(对于中小型流量站点),将HTML片段放到服务器端,并简单地替换AJAX回调函数中的容器内容(例如div内容)是完全可以的。 Of course, replacing the whole page would be silly, hence your Django application needs to support outputting specific regions of the document. 当然,替换整个页面很愚蠢,因此Django应用程序需要支持输出文档的特定区域。

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

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