Created
July 12, 2010 18:00
-
-
Save dvv/472821 to your computer and use it in GitHub Desktop.
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
exports.form: (uri, formdef, success) -> | |
form: forms.create formdef | |
handler: (request, nextApp) -> | |
#h.dir 'REQ:', request | |
if request.method is 'GET' | |
x: h.when nextApp(request), (y) -> | |
h.dir 'REQ:', y.body | |
form.bind y.body | |
response: h.defer() | |
respond: (body) -> | |
form.nonce: h.nonce() | |
#formstr: '<form method="post" action="' + (uri or '') + '">' + form.toHTML() + '<input type="hidden" name="_formkey" value="' + form.nonce + '" /><input type="submit" /></form>' | |
formstr: '<form class="crud" method="post">' + form.toHTML() + '<input type="hidden" name="_formkey" value="' + form.nonce + '" /><input type="submit" /></form>' | |
response.resolve { | |
status: 200 | |
headers: {'content-type': 'text/html'} | |
body: if body then body else [formstr] | |
} | |
values: request.body or {} | |
if request.method is 'POST' and values['_formkey'] is form.nonce | |
form.handle values, { | |
success: (f) -> | |
# do something with collected data | |
if typeof success is 'function' and success(f.data) | |
# reset form bindings | |
form: form.bind {} unless form.keepData | |
# respond with empty body -- means OK | |
respond [] | |
else | |
respond() | |
other: (f) -> | |
respond() | |
empty: (f) -> | |
respond() | |
} | |
else | |
respond() | |
response | |
args: Array.prototype.slice.call arguments, 1 | |
routes.crud uri, handler |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment