Created
June 8, 2012 08:02
-
-
Save jamesflorentino/2894377 to your computer and use it in GitHub Desktop.
Dynamic way of creating classes based on server response.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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