Last active
December 15, 2015 10:29
-
-
Save simonmcmanus/5245865 to your computer and use it in GitHub Desktop.
Get an API for free with Blend. After calling the middleware just call res.blend()
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
var _blend = function(req, res, next) { | |
res.blend = function(params) { | |
params.buildData(params.params, function(e, d) { | |
if(req.headers['content-type'] === 'application-json') { | |
res.json(d); | |
} else { // render html | |
params.buildSelectors(d, function(selectors) { | |
if(req.headers['content-type'] === 'sizlate') { | |
var options = { | |
layout: (req.param('_frax')) ? null : params.layout, | |
container: params.container || '#container', | |
selectors: selectors | |
}; | |
res.json({ | |
template: params.template, | |
selectors: options | |
}); | |
}else { | |
res.render(params.template, options); | |
} | |
}); | |
} | |
}); | |
}; | |
next(); | |
}; | |
app.use(_blend); |
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
app.get('/', function(req, res, next) { | |
res.blend({ | |
buildData: function(params, callback) { | |
GS.db.getRawData(params.id, callback); | |
}, | |
buildSelectors: function(data, callback) { | |
var selectors = GS.utils.generateGlobalSelectors(req); | |
selectors['#widgets'] = { | |
partial: 'target', | |
data: data | |
}; | |
callback(selectors); | |
GS.tracker.storeEvent('Targets:createTarget:load'); | |
}, | |
template: 'target', | |
layout: GS.config.ui.layout, | |
container: '#container' | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment