Skip to content

Commit

Permalink
Unified helpers application on the client.
Browse files Browse the repository at this point in the history
  • Loading branch information
shimaore committed May 30, 2012
1 parent c34c04a commit d1ceca8
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ skeleton = ->
ws_handlers = {}
helpers = {}

apply_helpers = (ctx) ->
for name, helper of helpers
do (name, helper) ->
if typeof helper is 'function'
ctx[name] = (args...) ->
args.push ctx
helper.apply ctx, args
else
ctx[name] = helper
ctx

app = context.app = Sammy() if Sammy?

context.get = ->
Expand Down Expand Up @@ -42,9 +53,7 @@ skeleton = ->
route = (r) ->
ctx = {app}

for name, helper of helpers
ctx[name] = ->
helper.apply(ctx, arguments)
apply_helpers ctx

app.get r.path, (sammy_context) ->
ctx.params = sammy_context.params
Expand All @@ -71,10 +80,7 @@ skeleton = ->
data: data
emit: context.emit

for name, helper of helpers
do (name, helper) ->
ctx[name] = ->
helper.apply(ctx, arguments)
apply_helpers ctx

switch settings['databag']
when 'this' then h.apply(data, [ctx])
Expand All @@ -86,4 +92,4 @@ skeleton = ->
@build = (version, settings) ->
String(skeleton)
.replace('version = null;', "version = '#{version}';")
.replace('settings = null;', "var settings = #{JSON.stringify settings};")
.replace('settings = null;', "var settings = #{JSON.stringify settings};")

0 comments on commit d1ceca8

Please sign in to comment.