Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting jQuery timeago plugin to recognize newly loaded DOM elements
    primarykey
    data
    text
    <p>I'm using the <a href="http://timeago.yarp.com" rel="nofollow">jquery timeago plugin</a> in my Rails 3 application. I have a comments section on my posts#show view that automatically refreshes every 30 seconds with the AJAX used in Railscasts episode #229 <a href="http://railscasts.com/episodes/229-polling-for-changes" rel="nofollow">"Polling for Changes"</a>. The _comment partial that is loaded by jQuery contains the created_at time of the comment which uses the timeago Rails helper method to create attr tags with the correct time format. </p> <p>When a comment is submitted and ajax loads the comment, the new comment DOM element isn't recognized by the jQuery timeago plugin. So instead of the time of the comment being displayed as "about a minute ago" it displays "2010-11-21 23:08:36 UTC".</p> <p>I've googled this problem of course and found suggestions regarding the use of .live(), .delegate() or the livequery plugin. </p> <p>comments/index.js.erb:</p> <pre><code>&lt;% unless @comments.empty? %&gt; $("#comments").append("&lt;%=raw escape_javascript(render(@comments)) %&gt;").timeago(); &lt;% end %&gt; </code></pre> <p>comments/show.js.erb:</p> <pre><code>$("#comments").append("&lt;%=raw escape_javascript(render(@comments)) %&gt;"); </code></pre> <p>public/javascripts/application.js:</p> <pre><code>$(function() { if ($("#comments").length &gt; 0) { setTimeout(updateComments, 30000); } }); function updateComments () { var post_id = $("#post").attr("data-id"); if ($("#comments").length &gt; 0) { var after = $(".comment:last-child").attr("data-time"); } else { var after = "0"; } $.getScript("/posts/" + post_id + "/comments?after=" + after) setTimeout(updateComments, 30000); } </code></pre> <p>helpers/application_help.rb:</p> <pre><code>module ApplicationHelper def timeago(time, options = {}) options[:class] ||= "timeago" content_tag(:abbr, time.to_s, options.merge(:title =&gt; time.getutc.iso8601)) if time end end </code></pre> <p>layouts/application.html.erb:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;%= stylesheet_link_tag 'style' %&gt; &lt;%= javascript_include_tag :defaults %&gt; &lt;%= javascript_include_tag 'jquery.timeago' %&gt; &lt;%= csrf_meta_tag %&gt; &lt;/head&gt; &lt;body&gt; &lt;!-- ... --&gt; &lt;script type="text/javascript"&gt; $("abbr.timeago").timeago(); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>comments/_comment.html.erb:</p> <pre><code>&lt;div class="comment" data-time="&lt;%= comment.created_at.to_i %&gt;"&gt; &lt;%- if comment.commenter.empty? -%&gt; &lt;%- else -%&gt; &lt;span class="name"&gt; &lt;%- if comment.email.blank? -%&gt; &lt;%= comment.commenter %&gt;: &lt;%- else -%&gt; &lt;a href="mailto:&lt;%= comment.email %&gt;"&gt;&lt;%= comment.commenter %&gt;:&lt;/a&gt; &lt;%- end -%&gt; &lt;/span&gt; &lt;%- end -%&gt; &lt;%= simple_format @comment.body %&gt; &lt;span class="time"&gt; &lt;%= timeago(comment.created_at) %&gt; &lt;/span&gt; &lt;/div&gt; </code></pre> <p>posts/show.html.erb:</p> <pre><code>&lt;div id="post" data-id="&lt;%= @post.id %&gt;"&gt; &lt;%= link_to @post.title, @post %&gt; &lt;%= simple_format @post.content %&gt; &lt;% unless @post.comments.empty? %&gt; &lt;div id="comments"&gt; &lt;%= render @post.comments %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;/div&gt; &lt;div id="replyform"&gt; &lt;%= render "comments/form" %&gt; &lt;/div&gt; </code></pre> <p>The AJAX polling and timeago plugin functionality works fine everywhere else, it's just when I make a comment and that comment appears with AJAX from another browser that I run into this issue. Any suggestions, resources or code would make my week. Thank you for reading my post.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload