简体   繁体   English

如何不阻塞地加载数据?

[英]How to load data without blocking?

I want to load items from a database in my template. 我想从模板中的数据库加载项目。 This proccess of loading can take some seconds, so I want to display the page before the proccess is done. 此加载过程可能需要几秒钟,因此我想在过程完成之前显示页面。

{% for item in get_items() %}
    <div> ... </div>
{% end %}

How can I load this data without blocking the page? 如何在不阻止页面的情况下加载此数据?

The easiest way would be two views: the view that's tied to your URL which just shows the "wrapper" around your actual data and has an HTML element in it like: 最简单的方法是两个视图:与您的URL绑定的视图,该视图仅显示实际数据周围的“包装”,并在其中包含一个HTML元素,例如:

<div id="my-content">(loading data)</div>

And, assuming you use jQuery, something like: 并且,假设您使用jQuery,则类似:

<script>
    $("#my-content").load("/path/to/view/that/provides/data");
</script>

And then have a second view wired to that URL. 然后将第二个视图连接到该URL。 This should return just the formatted HTML of the data itself (so possibly just that table or however you're presenting it). 这应该返回数据本身的格式化HTML(因此可能只返回该表,或者您要显示它)。

Of course, you can also do this not by having the second view provide formatted HTML; 当然,您也可以不通过让第二个视图提供格式化的HTML来做到这一点。 you could have it produce JSON or XML or some more data-y format and then format that with Javascript. 你可以有它产生的JSON或XML或更多的数据-Y格式,然后格式化使用Javascript。 For some cases, that can be more efficient. 在某些情况下,这可能更有效。

If you're not using jQuery, look up how to do a simple "fill this item with the response of this URL done via AJAX" call in whatever framework you do use--or you can do this in raw Javascript too, though somewhat more verbosely. 如果您不使用jQuery,请查找如何在使用的框架中进行简单的“通过AJAX完成此URL的响应来填充此项目”调用,或者也可以在原始Javascript中进行此操作,尽管有些更详细地说。

加载没有它们的页面,然后通过单独的Ajax调用将其添加。

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

相关问题 如何在不阻塞页面加载的情况下加载大型 Javascript 文件 - How to load a large Javascript file without blocking the page load 如何在不阻塞 onload 或等待 onload 的情况下加载 iframe - How to load an iframe without blocking onload or waiting for onload 大量计算和显示数据而不会阻塞 UI - Heavy calculations and displaying data without blocking UI 如何在不单击链接的情况下加载数据? - How to load the data without clicking on the link? 如何在没有解决的情况下将数据加载到服 - How to load data into service without resolve 如何不使用.get()或.load()将Ajax数据加载到DIV中? - How to load ajax data into DIV without using .get() or .load()? 在移动应用程序的 Javascript 中动态加载图像而不阻塞 UI? - Load images dynamically without blocking UI in Javascript on mobile application? 如何在 React Native 中进行预取并将数据持久化到本地存储而不阻塞 UI? - How to do prefetch in React Native and persist data to local storage without blocking the UI? 如何在浏览器中打开新标签页而不被阻止? - How open a new tab in browser without blocking? 当jquery加载大量数据时,为什么浏览器会阻塞? - Why browser is blocking when jquery load large amounts of data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM