Find file
Fetching contributors…
Cannot retrieve contributors at this time
91 lines (78 sloc) 3.39 KB
LiftState = require('../lift')
coffeekup = require('coffeekup') #
connect = require('connect')
{parse} = require('url')
# client side
template = -> # coffeekup
doctype 5
html ->
head ->
meta charset:'utf-8'
title @title or "lifted!"
style 'body {font-family: sans-serif}'
script src:''
script src:'' # because the client side part is using this as well
script {type:'text/javascript'}, @lift.code() # all needed boilerplate code. it also defines 'lift' in the global context; only need once
coffeescript -> # client code
$('document').ready ->
# requesting data for part 'test'
setTimeout ->
$.getJSON "?", lift:'test', (data) ->
$('#test-content').html'test', window.CoffeeKup, data)
, 2000 # 2sec delay
body ->
text @lift 'info', (txt) -> "#{txt}:"
div id:'hit-content', ->
@lift 'hit', @remoteAddr, (addr, ck, data) ->
console.log "lift" ,arguments
ck.render ->
text data.value
div style:'color:blue', ->
text "this was fast."
small style:'color:gray', ->
text "request by #{addr}"
,dynamic_locals:on, locals:{data, addr}
div id:'test-content', ->
text "* loading content …"
# defining the client side part
@lift 'test', @remoteAddr, (addr, ck, data) ->
console.log "test" ,arguments
ck.render ->
text data.value
div style:'color:red', ->
text "awesome!"
small style:'color:gray', ->
text "request by #{addr}"
,dynamic_locals:on, locals:{data, addr}
# server side
server = connect.createServer connect.logger(), (req, res) ->
{query, pathname} = parse(req.url, true)
unless pathname is "/"
res.statusCode = 404
return res.end()
state = new LiftState
# we want to render the given liftstate aspect on the server'hit',
{render:((f)->f())}, # ck - just a dummy because we are allready in coffeekup
{value:"other lift has a 2sec delay"}) # data - normal payload data
state.get 'info', (template) ->
template("lifting data to the next request layer")
if query.lift # ajax request …
res.setHeader('Content-Type', "application/json")
if query.lift is 'hit'
body = value:"forever alone data"
else if query.lift is 'test'
body = value:"laziness is"
body = JSON.stringify body
else # normal page …
res.setHeader('Content-Type', "text/html")
context =
body = coffeekup.render template, {context, format:on}
# normal http server foo
res.setHeader('Content-Length', body.length)
console.log "server listening on port 3000 …"