timeMessageBlockView.coffee 765 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. define [
  2. "libs/underscore"
  3. "libs/backbone"
  4. "moment"
  5. ], (_, Backbone, moment) ->
  6. ONE_WEEK = 7 * 24 * 60 * 60 * 1000
  7. TimeMessageBlockView = Backbone.View.extend
  8. className : "timeSinceMessage"
  9. initialize: () ->
  10. @autoRefresh()
  11. setTimeOnce: (timestamp)->
  12. if !@timestamp?
  13. @timestamp = timestamp
  14. @render()
  15. return @
  16. setTime: (@timestamp)->
  17. @render()
  18. return @
  19. autoRefresh: ->
  20. if @timestamp?
  21. @render()
  22. self = @
  23. doIt = =>
  24. self.autoRefresh()
  25. setTimeout doIt, 60 * 1000
  26. render: () ->
  27. milisecondsSince = new Date().getTime() - @timestamp
  28. if milisecondsSince > ONE_WEEK
  29. time = moment(@timestamp).format("D/MMM/YY, h:mm:ss a")
  30. else
  31. time = moment(@timestamp).fromNow()
  32. this.$el.html(time)