0

Can someone point me to some example code on how best to do caching while loading up the dynamic parts of a page using javascript?

My canonical example involves a page where nothing ever changes other than the user's logged in status and username at the top of the page. Changing this text through jQuery and an ajax request is easy enough, but since I'm doing it on $(document).ready(), if you look quickly, you can see the page load with something like "Click Here to Login" before the ajax request fires and updates that section of the page.

Edit for clarification:

If I cache the entire page, the following happens.

  1. User A requests the page
  2. Page is not cached, so application generates html with "Hi User A" in the upper right corner
  3. User B requests the page
  4. Page is cached, so application serves it us as-is -- including the "Hi User A" bit <- This is the part that I want to dynamically update.

Ideally, at step 2, the page would get cached without the "Hi User A" bit, so that when someone requests the page, I make a simple ajax request to get the greeting and then shove it into the dom.

I'm assuming javascript is the way to go, but I'm thinking there has to be a better way than to wait for $(document).ready() such that the page renders more naturally.

If it matters (though I don't think it should, as a solution that applies to static html would also work for what I'm doing), I'm using rails 2.3.x and jQuery.

4

2 回答 2

1

我最初会在服务器的主页上加载用户状态,使用与生成 AJAX 响应相同的东西。然后当用户更改状态时,使用 AJAX 客户端对其进行更新。

这样,初始页面具有正确的状态,但在用户执行操作时仍然可以更改。

假设该方法getStatus()为用户所处的任何状态生成正确的 HTML。在页面上,我将有一个<div>包含此信息的 HTML。服务器端,调用getStatus()(以 PHP 为例):

<div id="status"><?php =getStatus() ?></div>

然后让您的 AJAX 请求返回getStatus()并更新<div id="status">内容。

于 2010-12-22T21:10:18.177 回答
0

啊哈!有一个 railscast 正是我想要的!

Rails-y 的优点可以在这里找到:http ://railscasts.com/episodes/169-dynamic-page-caching

于 2011-01-14T17:02:30.097 回答