Created
May 14, 2011 04:57
-
-
Save jmoyers/971933 to your computer and use it in GitHub Desktop.
This file contains 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
Model = require('./model') | |
validator = require('validator') | |
sanitize = validator.sanitize | |
_ = require('underscore') | |
m = new Model({ | |
test : '', | |
num : 0 | |
}) | |
dump = (label)-> | |
return (attribs, cb)-> | |
cb or= ()-> | |
console.log '===' + label + '===' | |
console.log attribs | |
cb(attribs) | |
m.on('change', dump('general change')) | |
m.on('test.change', dump('test changed')) | |
m.on('num.change', dump('num changed')) | |
# dump attributes being set | |
m.use dump('start middleware') | |
# define a generic html entity encode middleware | |
m.use (attribs, cb)-> | |
for k,v of attribs | |
attribs[k] = sanitize(v).entityEncode() | |
cb(attribs) | |
# define a property specific validator middleware | |
m.use 'num', (val, cb)-> | |
cb(sanitize(val).toInt()) | |
# dump properties at the end of the middleware chain for inspection | |
m.use dump('end middlware') | |
# use fire and forget setters | |
m.test = '<some>sweet</html>' | |
m.num = '0123' | |
# use setters with a callback that gives output after mw | |
m.set('test', 'this is indeed a <a href="test">test</a>', (attribs)-> | |
# attributes after they've been committed | |
) |
This file contains 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
===start middleware=== | |
{ test: '<some>sweet</html>' } | |
===start middleware=== | |
{ num: '0123' } | |
===start middleware=== | |
{ test: 'this is indeed a <a href="test">test</a>' } | |
===end middlware=== | |
{ test: '<some>sweet</html>' } | |
===end middlware=== | |
{ test: 'this is indeed a <a href="test">test</a>' } | |
===general change=== | |
{ id: 'E12E0D5D-6054-48A5-B2A5-A5C0B8654331', | |
test: '<some>sweet</html>', | |
num: 0 } | |
===test changed=== | |
<some>sweet</html> | |
===end middlware=== | |
{ num: 123 } | |
===general change=== | |
{ id: 'E12E0D5D-6054-48A5-B2A5-A5C0B8654331', | |
test: 'this is indeed a <a href="test">test</a>', | |
num: 0 } | |
===test changed=== | |
this is indeed a <a href="test">test</a> | |
===general change=== | |
{ id: 'E12E0D5D-6054-48A5-B2A5-A5C0B8654331', | |
test: 'this is indeed a <a href="test">test</a>', | |
num: 123 } | |
===num changed=== | |
123 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment