jQuery, TimeAgo and Label

Good morning,

I managed including the jQuery plugin
into Vaadin using an extension of the Label component. It’s simple way of doing so, no
involved. The constructor requires an
DateTime instance:

public class TimeAgoLabel extends Label {

  public static final String JQUERY_TIMEAGO_SCRIPT = "jQuery('time.timeago').timeago();";

  static DateTimeFormatter isoFormatter = ISODateTimeFormat.dateTime();
  static DateTimeFormatter localFormatter = DateTimeFormat.forPattern("dd.MM.yyyy hh:mm");

  public TimeAgoLabel(DateTime datetime) {
    String iso = isoFormatter.print(datetime);
    String loc = localFormatter.print(datetime);
    setValue(format("<time class='timeago' datetime='%s'>%s</time>", iso, loc));

The usage in an UI class could look like this:

protected void init(VaadinRequest request) {
  setContent(new TimeAgoLabel(new DateTime()));

So, after an instance of TimeAgoLabel was added to the DOM, we have to trigger the jQuery script that converts the instant in time to its readable form. This also applies to after TimeAgoLabels are inserted by

Is there a better, more robust, more Vaadin 7 way to include timeago? How is the “Latest post” text in this forum (not the scrolling effect) achieved?

Have fun,