Created
June 24, 2012 01:59
-
-
Save shimaore/2980998 to your computer and use it in GitHub Desktop.
Pass CoffeeCup params down to embedded CoffeeScript
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
require('zappajs') -> | |
@use 'zappa' | |
@enable 'default layout' | |
@get '/': -> | |
foo = 'bar' | |
@render 'index', foo: foo, hardcode: { coffee: export_params } | |
@view index: -> | |
@scripts = ['/zappa/jquery'] | |
coffee -> | |
$(document).ready -> | |
alert params.foo | |
# Embed the @render params into window.params (aka `params` since | |
# window is the client-side Javascript context). | |
export_params = (coffee) -> | |
text "<script> window.params = {};" | |
for name, value of @ | |
try text """ | |
window.params[#{JSON.stringify name}] = #{JSON.stringify value}; | |
""" | |
text "</script>" | |
coffeescript coffee |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment