diff --git a/foo.coffee b/foo.coffee index ccd7384..4b795f4 100644 --- a/foo.coffee +++ b/foo.coffee @@ -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 \ No newline at end of file diff --git a/src/client.coffee b/src/client.coffee index f9864ae..7681f9d 100644 --- a/src/client.coffee +++ b/src/client.coffee @@ -4,7 +4,6 @@ skeleton = -> zappa.version = null coffeescript_helpers = null - copy_data_to = null settings = null zappa.run = (func) -> @@ -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]) @@ -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};") \ No newline at end of file diff --git a/src/zappa.coffee b/src/zappa.coffee index c82b99f..6a2f052 100644 --- a/src/zappa.coffee +++ b/src/zappa.coffee @@ -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. @@ -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 @@ -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) -> diff --git a/tests/assets.coffee b/tests/assets.coffee index 1f5c467..b7d6bd7 100644 --- a/tests/assets.coffee +++ b/tests/assets.coffee @@ -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' @@ -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' @@ -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) ->