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