开发者

Rails & Javascript - How to mark something as viewed or read?

How to mark something as viewed when a user clicks on a row. Here is an example what I am trying to explain: http://www.dba.dk/dyr/hunde-og-tilbehoer/racehunde/race-labrador/ Try to click on a row and go back. You will see the row is marked with a check sign. So you know you h开发者_开发百科ave seen it.

How to create something like it?


You're looking to save an AJAX history state. Two Amazing Rails and Javascript tutorials can be found here :

  1. http://railscasts.com/episodes/246-ajax-history-state

  2. http://railscasts.com/episodes/175-ajax-history-and-bookmarks

The first link will reference the second.

From Ryan Bate's Website | Railscasts.com

/* application.js */
if (history && history.pushState) {
  $(function() {
    $("#products th a, #products .pagination a").live("click", function(e) {
      $.getScript(this.href);
      history.pushState(null, document.title, this.href);
      e.preventDefault();
    });
    $("#products_search input").keyup(function() {
      $.get($("#products_search").attr("action"), $("#products_search").serialize(), null, "script");
      history.replaceState(null, document.title, $("#products_search").attr("action") + "?" + $("#products_search").serialize());
    });
    $(window).bind("popstate", function() {
      $.getScript(location.href);
    });
  });
}

/* products/index.js.erb */
$("#products").html("<%= escape_javascript(render("products")) %>");
document.title = "<%= escape_javascript("#{params[:search].to_s.titleize} Products by #{(params[:sort] || 'name').titleize} - Page #{params[:page] || 1}") %>";
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜