PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/public/js/scripts/index.coffee

https://bitbucket.org/laurenceputra/codegether
CoffeeScript | 115 lines | 101 code | 11 blank | 3 comment | 0 complexity | e32251b673a226bfd03f59c76357e455 MD5 | raw file
  1. define ['jquery', 'underscore', 'backbone', 'text!templates/msg.underscore'], ($, _, Backbone, msgTemplate) ->
  2. App = {}
  3. App.Room = {}
  4. App.Message = {}
  5. App.User = {}
  6. window.App = App
  7. App.User.model = Backbone.Model.extend
  8. defaults:
  9. name: "John Doe" #yes, you're dead
  10. nick: null
  11. thumbnail: null #img url?
  12. App.User.view = Backbone.View.extend
  13. model: App.User.model
  14. initialize: () ->
  15. _.bindAll @, 'render'
  16. template: {}#TODO
  17. render: () ->
  18. App.User.collection = Backbone.Collection.extend
  19. model: App.User.model
  20. url: '/:room/users'
  21. initialize: () ->
  22. App.Message.model = Backbone.Model.extend
  23. defaults:
  24. room: null#an App.Room
  25. timestamp: null
  26. message: null
  27. type: null # {0:text, 1:image, 2:code, 3:markdown, ...}
  28. user: null # App.User.model
  29. initialize: () ->
  30. App.Message.view = Backbone.Model.extend
  31. el: 'li'
  32. className: 'chat-entry'
  33. model: App.Message.model
  34. initialize: () ->
  35. _.bindAll @, 'render'
  36. template: _.template msgTemplate
  37. render: () ->
  38. data =
  39. type: @model.get 'type'
  40. message: @model.get 'message'
  41. user: @model.get('user').get('nick')
  42. $el.html(@template data)
  43. App.Message.collection = Backbone.Collection.extend
  44. model: App.Message.model
  45. App.Room.model = Backbone.Model.extend
  46. url: '/:room'
  47. defaults:
  48. createdAt: null
  49. createdBy: null
  50. allowed: null
  51. name: "New Chat Room"
  52. welcomeMsg: null
  53. messages: null#a collection of App.message
  54. users: null#a collection of App.user
  55. initialize: (options) ->
  56. @users = new App.User.collection()
  57. @users.bind 'add', options.newUserHandler
  58. @users.fetch
  59. success: () -> options.successHandler "Users were successfully retreived."
  60. error: () -> options.errorHandler "Failed to retreive users."
  61. @messages = new App.Message.collection()
  62. @messages.bind 'add', options.newMessageHandler
  63. @messages.fetch
  64. success: () -> options.successHandler "Messages were successfully retreived."
  65. error: () -> options.errorHandler "Failed to retreive messages."
  66. App.Room.view = Backbone.View.extend
  67. el: 'div#chat-wrapper'
  68. model: App.Room.model
  69. initialize: () ->
  70. that = @
  71. _.bindAll @, 'render', 'notifyError', 'notifySuccess', 'addNewMessage', 'addNewUser', 'scrollChatView'
  72. @model = new App.Room.model
  73. newMessageHandler: @addNewMessage
  74. newUserHandler: @addNewUser
  75. successHandler: @notifySuccess
  76. errorHandler: @notifyError
  77. @model.fetch
  78. success: @notifySuccess "Chat room retreived successfully =)"
  79. error: @notifyError "Failed to retreive chat room =("
  80. @delegateEvents @events
  81. @w = #wrappers
  82. $msgs: $('#chat-contents', $el)
  83. $users: $('#chat-participants', $el)
  84. events: {
  85. }
  86. scrollChatView: () ->
  87. @w.msgs.animate scrollTop: @w.msgs.prop('scrollHeight') - @w.msgs.height(), 600
  88. render: () ->
  89. #TODO::
  90. addNewMessage: (msg) ->
  91. #takes in App.Message.model
  92. view = new App.Message.view
  93. model: msg
  94. @w.$msgs.append view.render()
  95. @scrollChatView()
  96. addNewUser: (user) ->
  97. #takes in App.User.model
  98. view = new App.User.view
  99. model: msg
  100. @w.$users.append view.render()
  101. notifyError: (text) ->
  102. console.log "ERROR :: #{text}"
  103. notifySuccess: (text) ->
  104. console.log "SUCCESS :: #{text}"