Skip to content

Instantly share code, notes, and snippets.

@shimaore
Created June 24, 2012 01:59
Show Gist options
  • Save shimaore/2980998 to your computer and use it in GitHub Desktop.
Save shimaore/2980998 to your computer and use it in GitHub Desktop.
Pass CoffeeCup params down to embedded CoffeeScript
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