Skip to content

Instantly share code, notes, and snippets.

@mauricemach
mauricemach / nested.coffee
Created October 25, 2010 00:37
User-defined nested rendering, synchronous
ck = require 'coffeekup'
tpl = ->
html ->
head ->
title 'title'
body ->
render 'index.coffee'
render = (file) ->
ck = require 'coffeekup'
template = ->
doctype 5
html ->
head ->
title 'title'
body ->
render @user_panel, user: @user
@mauricemach
mauricemach / realtime_tracking.coffee
Created November 11, 2010 20:39
Proof of concept on realtime visits tracking.
express = require 'express'
io = require 'socket.io'
ck = require 'coffeekup'
app = express.createServer()
app.get '/', (req, res) ->
res.send ck.render ->
@title = 'WebSockets tracker'
doctype 5
html ->
@mauricemach
mauricemach / zappa_partials_ugly.coffee
Created November 16, 2010 20:18
"Faking" partials with what we have in zappa today
get '/': ->
@items = [
{name: 'coffeescript', url: 'http://coffeescript.org'}
{name: 'ruby', url: 'http://ruby-lang.org'}
{name: 'python', url: 'http://python.org'}
]
# I think helpers belong in the template's locals "namespace",
# but we cannot currently specify the template's locals in zappa.
@partial = partial
@mauricemach
mauricemach / zappa_partials_beautiful.coffee
Created November 16, 2010 20:20
Proposed API for partials.
get '/': ->
@items = [
{name: 'coffeescript', url: 'http://coffeescript.org'}
{name: 'ruby', url: 'http://ruby-lang.org'}
{name: 'python', url: 'http://python.org'}
]
render 'default'
view ->
helper requires_login: (cb) ->
if request.session.user then cb()
else redirect "/sessions/new?redir=" + request.url
get "/accounts/new": ->
requires_login ->
render "home/accountNew"
@mauricemach
mauricemach / gist:1205485
Created September 9, 2011 04:13
benchmark of scope manipulation methods in node.js
log = console.log
ben = require 'ben'
vm = require 'vm'
times = 10000
global.sum = (x, y) -> x + y
vm_script = vm.createScript 'console.log(typeof require);', 'vm_script'
new_function = new Function('locals', 'console.log(typeof require);')
applied = -> console.log(typeof require)
zappa =
run: (f)->
# Re-compile the function in a context that contains the Zappa
# methods as globals.
app = require('express').createServer()
vm = require 'vm'
code = 'zappa_code='+f.toString()
views = {}
router = {}
# auth_helper.coffee
@include = ->
helper auth: (cb) ->
if session.user
@user = session.user
cb()
else
redirect '/login'
# account_controller.coffe
require('zappa') ->
before = (req, res, next) ->
console.log 'before'
next()
@use before
@get '/': ->
console.log 'route'
'route'