2013-04-30 05:26:09 +08:00
|
|
|
define [
|
|
|
|
'underscore',
|
|
|
|
'backbone',
|
|
|
|
'globals',
|
|
|
|
'cs!models/user'
|
|
|
|
], (_, Backbone, globals, UserModel) ->
|
|
|
|
|
|
|
|
# TODO: this class should actually store UserModel's, for now it is only trigerring events
|
|
|
|
UsersCollection = Backbone.Collection.extend
|
|
|
|
model: UserModel
|
|
|
|
|
|
|
|
initialize: ->
|
|
|
|
|
|
|
|
start: ->
|
2013-05-21 02:47:18 +08:00
|
|
|
# TODO: this should be in `initialize`, but can't be right now because
|
|
|
|
# globals.connection doesn't exist yet
|
2013-05-21 04:41:29 +08:00
|
|
|
# Bind to the event triggered when the client connects to the server
|
|
|
|
if globals.connection.isConnected()
|
|
|
|
@_registerEvents()
|
|
|
|
else
|
|
|
|
globals.events.on "connection:connected", =>
|
|
|
|
@_registerEvents()
|
2013-04-30 05:26:09 +08:00
|
|
|
|
|
|
|
_registerEvents: ->
|
|
|
|
|
|
|
|
globals.events.on "connection:user_list_change", (users) =>
|
|
|
|
globals.events.trigger("users:user_list_change", users)
|
|
|
|
|
|
|
|
globals.events.on "connection:load_users", (users) =>
|
2013-11-01 07:30:37 +08:00
|
|
|
for userBlock in users
|
2013-06-26 23:15:01 +08:00
|
|
|
@add [
|
|
|
|
id : userBlock.id
|
|
|
|
userid: userBlock.id
|
|
|
|
username: userBlock.name
|
|
|
|
]
|
2013-04-30 05:26:09 +08:00
|
|
|
globals.events.trigger("users:load_users", users)
|
2013-11-01 07:30:37 +08:00
|
|
|
|
2013-04-30 05:26:09 +08:00
|
|
|
globals.events.on "connection:user_join", (userid, username) =>
|
2014-04-04 02:34:19 +08:00
|
|
|
console.log "users.coffee: on(connection:user_join)" + username
|
2013-06-26 23:15:01 +08:00
|
|
|
@add [
|
|
|
|
id : userid
|
|
|
|
userid: userid
|
|
|
|
username: username
|
2013-11-01 07:30:37 +08:00
|
|
|
]
|
2013-04-30 05:26:09 +08:00
|
|
|
globals.events.trigger("users:user_join", userid, username)
|
2013-11-01 07:30:37 +08:00
|
|
|
|
2013-04-30 05:26:09 +08:00
|
|
|
globals.events.on "connection:user_leave", (userid) =>
|
2013-06-26 23:15:01 +08:00
|
|
|
toDel = @get(userid)
|
|
|
|
@remove(toDel)
|
2013-04-30 05:26:09 +08:00
|
|
|
globals.events.trigger("users:user_leave", userid)
|
|
|
|
|
2014-04-03 02:20:41 +08:00
|
|
|
globals.events.on "connection:user_left", (userid) =>
|
|
|
|
toDel = @get(userid)
|
|
|
|
@remove(toDel)
|
|
|
|
globals.events.trigger("users:user_left", userid)
|
|
|
|
|
2013-11-01 07:30:37 +08:00
|
|
|
globals.events.on "connection:setPresenter", (userid) =>
|
2013-04-30 05:26:09 +08:00
|
|
|
globals.events.trigger("users:setPresenter", userid)
|
|
|
|
|
|
|
|
UsersCollection
|