Skip to content

Instantly share code, notes, and snippets.

@simonmcmanus
Last active December 15, 2015 10:29
Show Gist options
  • Save simonmcmanus/5245865 to your computer and use it in GitHub Desktop.
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()
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);
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