开发者

What's the recommended way to rebuild a page from the browser history? (History.js+Rails 3)

开发者 https://www.devze.com 2023-04-03 21:25 出处:网络
Many of the pages in my Rails 3 application are composed by widgets. For example there is a user page with url /users/12 that invokes the users controller show action. This page loads the feed widget

Many of the pages in my Rails 3 application are composed by widgets. For example there is a user page with url /users/12 that invokes the users controller show action. This page loads the feed widget that makes an ajax call to the feed controller in order to get all the feed items for the user.

So lets' suppose the user does the following:

1) enters in the brow开发者_如何学Pythonser http://mydomain.com/users/12

2) clicks on a button "Today's posts" to see just today's post (this triggers an ajax call to the feed controller that returns the posts

3) enter http://google.com

What is the recommended way to handle the browser's history and back button?

I guess what I need to do is to store enough state information so that when the user clicks on the back button the browser loads the users/12 page and then clicks on the today's filter button again. What is the best way to implement this?

I could store a pointer to a function to be invoked:

History.pushState({"function_name": "restoreUserHomePage"}, "", /users/12/feed?time=today);

Bad idea? Good idea?

Also what is the "right" way to register for the stateChanged (popState in HTML5) event. Potentially each of my page has a different set of widgets.

Thanks for any help.


The problem actually is that your browser is caching the xhr request. So when you press the back button, it loads the .js format, instead of the full url that you have if your browser.

Here is how I fixed it :

  #in application_controller
  before_filter :set_cache_buster  
  def set_cache_buster
    if request.xhr?
      response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
      response.headers["Pragma"] = "no-cache"
      response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
    end
  end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号