Permalink
Browse files

Making `@on` available more widely on the client (#51)

  • Loading branch information...
1 parent b1456b8 commit 10dfb4d5bc6f3f5b772ccb4425f8579fd1515a25 @shimaore shimaore committed Jan 4, 2013
Showing with 23 additions and 28 deletions.
  1. +10 −10 examples/chat.coffee
  2. +13 −18 src/client.coffee
View
@@ -1,31 +1,31 @@
require('./zappajs') ->
-
+
@get '/': ->
@render index: {layout: no}
-
+
@on 'set nickname': ->
@client.nickname = @data.nickname
-
+
@on said: ->
@broadcast said: {nickname: @client.nickname, text: @data.text}
@emit said: {nickname: @client.nickname, text: @data.text}
-
+
@client '/index.js': ->
@connect()
- @on said: ->
- $('#panel').append "<p>#{@data.nickname} said: #{@data.text}</p>"
-
$ =>
@emit 'set nickname': {nickname: prompt 'Pick a nickname!'}
-
+
+ @on said: ->
+ $('#panel').append "<p>#{@data.nickname} said: #{@data.text}</p>"
+
$('#box').focus()
-
+
$('button').click (e) =>
@emit said: {text: $('#box').val()}
$('#box').val('').focus()
e.preventDefault()
-
+
@view index: ->
doctype 5
html ->
View
@@ -9,7 +9,6 @@ skeleton = ->
context = {}
# Storage for the functions provided by the user.
- ws_handlers = []
helpers = {}
apply_helpers = (ctx) ->
@@ -36,8 +35,19 @@ skeleton = ->
helpers[k] = v
context.on = (obj) ->
- for message, action of obj
- ws_handlers.push {message,action}
+ for message, action of obj
+ context.socket.on message, (data) ->
+ ctx =
+ app: app
+ socket: context.socket
+ id: context.socket.id
+ data: data
+ emit: context.emit
+ share: context.share
+
+ apply_helpers ctx
+
+ action.apply ctx
context.connect = ->
context.socket = io.connect.apply io, arguments
@@ -73,21 +83,6 @@ skeleton = ->
context.share '__local', context.socket, (data) ->
context.key = data.key
- for {message,action} in ws_handlers
- do (message, action) ->
- context.socket.on message, (data) ->
- ctx =
- app: app
- socket: context.socket
- id: context.socket.id
- data: data
- emit: context.emit
- share: context.share
-
- apply_helpers ctx
-
- action.apply ctx
-
$(-> app.run '#/') if app?
@build = (version, settings) ->

0 comments on commit 10dfb4d

Please sign in to comment.