Created
September 9, 2011 00:49
-
-
Save shimaore/1205211 to your computer and use it in GitHub Desktop.
(proof-of-concept) Zappa with runInContext() to provide glocal vars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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