Skip to content

Commit

Permalink
Removed autoimport/autoexport and added databag. See #84.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricemach committed Sep 20, 2011
1 parent 19350a4 commit 97c7ba2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
18 changes: 13 additions & 5 deletions foo.coffee
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
require('./src/zappa') ->
@enable 'default layout'

@set views: __dirname + '/tests/views'
@set databag: 'param'

@get '/': ->
@render index: {foo: 'bong'}
@render 'index'

@on connection: ->
@emit welcome: {motd: 'data in the param!'}

@client '/index.js': ->
@connect()

@on welcome: (d) ->
console.log 'welcome:', d.motd

@view index: ->
@title = 'shaboo'
p 'inline view'
@title = 'Crazy zappa experiment'
@scripts = ['/socket.io/socket.io', '/zappa/zappa', '/index']
p @foo
20 changes: 9 additions & 11 deletions src/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ skeleton = ->
zappa.version = null

coffeescript_helpers = null
copy_data_to = null
settings = null

zappa.run = (func) ->
Expand Down Expand Up @@ -53,10 +52,10 @@ skeleton = ->
ctx.sammy_context = sammy_context
ctx.render = -> sammy_context.render.apply sammy_context, arguments
ctx.redirect = -> sammy_context.redirect.apply sammy_context, arguments
if settings['autoimport']
# Imports input vars to ctx. Does NOT overwrite existing variables.
copy_data_to ctx, [sammy_context.params]
r.handler.apply(ctx, [ctx])
switch settings['databag']
when 'context' then r.handler.apply(sammy_context.params, [ctx])
when 'param' then r.handler.apply(ctx, [sammy_context.params])
else r.handler.apply(ctx, [ctx])

# GO!!!
func.apply(context, [context])
Expand All @@ -72,22 +71,21 @@ skeleton = ->
id: context.socket.id
data: data
emit: context.emit

if settings['autoimport']
copy_data_to ctx, [data]

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

h.apply(ctx, [ctx])
switch settings['databag']
when 'context' then h.apply(data, [ctx])
when 'param' then h.apply(ctx, [data])
else h.apply(ctx, [ctx])

$(-> app.run '#/') if app?

@build = (version, coffeescript_helpers, copy_data_to, settings) ->
@build = (version, coffeescript_helpers, settings) ->
String(skeleton)
.replace('version = null;', "version = '#{version}';")
.replace('coffeescript_helpers = null;', "var coffeescript_helpers = '#{coffeescript_helpers}';")
.replace('copy_data_to = null;', "var copy_data_to = #{copy_data_to};")
.replace('settings = null;', "var settings = #{JSON.stringify settings};")
32 changes: 14 additions & 18 deletions src/zappa.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,8 @@ zappa.app = (func) ->
args[1] ?= {}
args.splice 1, 0, {} if typeof args[1] is 'function'

if app.settings['autoexport']
# Automatically send request input vars to template.
args[1].params = {}
for k, v of ctx
# TODO: What if I *want* to pass, say, @request to the view?
args[1].params[k] = v unless k in names
if app.settings['databag']
args[1].params = data

if args[1].postrender?
# Apply postrender before sending response.
Expand All @@ -283,16 +279,15 @@ zappa.app = (func) ->
args.push ctx
helper.apply ctx, args

# Names of non-input context vars.
names = []
names.push k for k, v of ctx

if app.settings['autoimport']
# Imports input vars to ctx. Does NOT overwrite existing variables.
copy_data_to ctx, [req.query, req.params, req.body]
if app.settings['databag']
data = {}
copy_data_to data, [req.query, req.params, req.body]

# Go!
result = r.handler.apply(ctx, [ctx])
switch app.settings['databag']
when 'context' then result = r.handler.apply(data, [ctx])
when 'param' then result = r.handler.apply(ctx, [data])
else result = r.handler.apply(ctx, [ctx])

res.contentType(r.contentType) if r.contentType?
if typeof result is 'string' then res.send result
Expand Down Expand Up @@ -342,15 +337,16 @@ zappa.app = (func) ->
socket.on name, (data) ->
ctx = build_ctx()
ctx.data = data
if app.settings['autoimport']
copy_data_to ctx, [data]
h.apply(ctx, [ctx])
switch app.settings['databag']
when 'context' then h.apply(data, [ctx])
when 'param' then h.apply(ctx, [data])
else h.apply(ctx, [ctx])

# Go!
func.apply(context, [context])

# The stringified zappa client.
client = require('./client').build(zappa.version, coffeescript_helpers, copy_data_to, app.settings)
client = require('./client').build(zappa.version, coffeescript_helpers, app.settings)

if app.settings['serve zappa']
app.get '/zappa/zappa.js', (req, res) ->
Expand Down
6 changes: 3 additions & 3 deletions tests/assets.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ port = 15200
c = t.client(zapp.app)
c.get '/zappa/zappa.js', (err, res) ->
t.equal 'content-type', res.headers['content-type'], 'application/javascript'
t.equal 'length', res.headers['content-length'], '5525'
t.equal 'length', res.headers['content-length'], '5574'

'zappa (automatic)': (t) ->
t.expect 'content-type', 'length'
Expand All @@ -135,7 +135,7 @@ port = 15200
c = t.client(zapp.app)
c.get '/zappa/zappa.js', (err, res) ->
t.equal 'content-type', res.headers['content-type'], 'application/javascript'
t.equal 'length', res.headers['content-length'], '5525'
t.equal 'length', res.headers['content-length'], '5574'

minify: (t) ->
t.expect 'zappa', 'client', 'shared', 'coffee', 'js'
Expand All @@ -150,7 +150,7 @@ port = 15200

c = t.client(zapp.app)
c.get '/zappa/zappa.js', (err, res) ->
t.equal 'zappa', res.headers['content-length'], '2809'
t.equal 'zappa', res.headers['content-length'], '2894'
c.get '/client.js', (err, res) ->
t.equal 'client', res.headers['content-length'], '42'
c.get '/shared.js', (err, res) ->
Expand Down

0 comments on commit 97c7ba2

Please sign in to comment.