Skip to content

Instantly share code, notes, and snippets.

@shimaore
Created September 9, 2011 00:49
Show Gist options
  • Save shimaore/1205211 to your computer and use it in GitHub Desktop.
Save shimaore/1205211 to your computer and use it in GitHub Desktop.
(proof-of-concept) Zappa with runInContext() to provide glocal vars
zappa =
run: (f)->
# Re-compile the function in a context that contains the Zappa
# methods as globals.
vm = require 'vm'
code = 'zappa_code='+f.toString()
views = {}
router = {}
context =
view: (name,code) ->
views[name] = code
render: (name) ->
views[name].apply(this_object)
get: (location,code) ->
router[location] = code
try
vm.runInNewContext code, context, 'zappa.run() in bob.coffee'
catch error
console.log "Error = #{error}"
context.zappa_code()
# The following code gets called from the httpServer's router.
this_object = # Populated from request, etc.
params:
foo: 3
bar: 5
request:
url: 'http://localhost'
console.log router['/'].apply(this_object)
zappa.run ->
get '/', ->
render 'bob'
view 'bob', ->
"Hello bob, #{@params.foo}"
zappa =
run: (f)->
vm = require 'vm'
code = 'zappa_code='+f.toString()
this_object =
params:
foo: 3
bar: 5
request:
url: 'http://localhost'
views = {}
router = {}
context =
view: (name,code) ->
views[name] = code
render: (name) ->
views[name].apply(this_object)
get: (location,code) ->
router[location] = code
try
vm.runInNewContext code, context, 'zappa.run() in bob.coffee'
catch error
console.log "Error = #{error}"
context.zappa_code()
console.log router['/'].apply(this_object)
zappa.run ->
get '/', ->
render 'bob'
view 'bob', ->
"Hello bob, #{@params.foo}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment