Question

So I'm using timeago plugin in my Rails 3 app, wrapping it within this function (note than the timeout just keeps the timeago strings updated to the minute at every moment):

function doTimeago() {
  $('.timeago').each(function() {
    var $this = $(this);
    if ($this.data('active')!='yes') {
      $this.timeago().data('active','yes');
    }
  });
}

And then in my application.js

$(function() {
  doTimeago();
}

This works great until I load some elements using remote calls. I researched a bit and found no working solution. I'm not happy adding livequery plugin as suggested in this question since it seems deprecated.

I was thinking of adding this JS code to the end of every js.erb file in my app, but it feels really duplicated and nasty.

doTimeago();

Question part 1: ¿Is there an easy way to inject that code after every js.erb execution?

Question part 2: ¿How do I achieve my primary goal of having ajaxy loaded elements work with timeago?

Was it helpful?

Solution

You can bind it to ajaxComplete event, like this:

$(document).on('ajaxComplete', function(){
  do_timeago();
});

BTW, I didn't understand your timeout in do_timeago function. Also, JS best practices are a bit different than ruby ones, consider rename your function to something like doTimeago.

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top