Skip to content

Instantly share code, notes, and snippets.

@jamesflorentino
Created June 8, 2012 08:02
Show Gist options
  • Save jamesflorentino/2894377 to your computer and use it in GitHub Desktop.
Save jamesflorentino/2894377 to your computer and use it in GitHub Desktop.
Dynamic way of creating classes based on server response.
class GameModel
constructor: ->
classObjects = {}
@register = (name, classObject) -> classObjects[name] = classObject
@create = (name) -> new classObjects[name]
@ready()
ready: -> return
class Client extends GameModel
ready: ->
# register all the possible classNames
@register 'human', Human
@register 'elf', Elf
@register 'dwarf', Dwarf
# Assume that SocketConnection is like socket.io
socket = new SocketConnection()
socket.on 'addUnit', @addUnit
addUnit: (attributes) =>
{code} = attributes
unit = @create code
window.onload = -> new Client()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment