newtheatre/history-project

View on GitHub
_coffee/app/RelativeTime.coffee

Summary

Maintainability
Test Coverage
class RelativeTime
  constructor: (opts) ->
    @el = opts.el
    @render()

  format: ->
    moment(@el.dataset.date, "YYYY-MM-DD").startOf('day').fromNow()

  render: ->
    @el.innerHTML = @format()

document.addEventListener 'turbolinks:load', ->
  relTimeEls = document.querySelectorAll('[data-date]')
  for relTimeEl in relTimeEls
    new RelativeTime
      el: relTimeEl