Skip to content

Instantly share code, notes, and snippets.

@laser
Last active August 29, 2015 14:11
Show Gist options
  • Save laser/39fa71fd120123249837 to your computer and use it in GitHub Desktop.
Save laser/39fa71fd120123249837 to your computer and use it in GitHub Desktop.
Cleaning up anonymous function expressions w/underscore
exports.index = function(req, res) {
async.waterfall([
//
// validate the search string
//
function(cb) {
// paramValidate protocol :
// url, sort ording, sorting field, searching field, valid attributes and callback
util.paramValidate(req.url, 'desc', 'id', Models.City.attributes, function (err, params) {
cb(err, params);
});
},
//
// getting total row count by query values
//
function(params, cb) {
// sqlCount protocol :
// url, sort ording, sorting field, searching field, valid attributes and callback
util.sqlCount(params, 'Cities', Models.City, validFields, 2, {},
function(err, count) {
cb(err, params, count);
});
},
//
// seeking all the row(s) by query values
//
function(params, count, cb) {
if (count > 0) {
// sqlSelect protocol :
// url, sort ording, sorting field, searching field, valid attributes and callback
util.sqlSelect(params, 'Cities', Models.City, validFields, 2, {}, undefined,
function(err, result) {
cb(err, {
"params": params,
"total_rows":count,
"data": _.map(result, function(item){ return item.selectedValues; })
});
});
} else cb(null, {"params": params, "total_rows":count, "data" : []});
}
],
//
// final return
//
function(err, result) {
if (err) {
res.send(err.status, err.msg);
} else {
res.send(200, result);
}
});
};
util.paramValidate(req.url, 'desc', 'id', Models.City.attributes).then(function(params) {
return util.sqlCount(params, 'Cities', Models.City, validFields, 2, {}).then(function(count) {
if (count > 0) {
// sqlSelect protocol :
// url, sort ording, sorting field, searching field, valid attributes and callback
return util.sqlSelect(params, 'Cities', Models.City, validFields, 2, {}, undefined).then(function(result) {
return Q({
"params": params,
"total_rows": count,
"data": _.map(result, function(item){ return item.selectedValues; })
});
});
}
else {
return Q({
params: params,
total_rows: count,
data: []
});
}
});
}).then(function(result) {
res.send(200, result);
}).fail(function(err) {
res.send(err.status, err.msg);
});
exports.index = function(req, res) {
async.waterfall([
//
// validate the search string
//
// paramValidate protocol :
// url, sort ording, sorting field, searching field, valid attributes and callback
_.partial(util.paramValidate, req.url, 'desc', 'id', Models.City.attributes),
//
// getting total row count by query values
//
function(params, cb) {
// sqlCount protocol :
// url, sort ording, sorting field, searching field, valid attributes and callback
util.sqlCount(params, 'Cities', Models.City, validFields, 2, {}, _.partial(cb, _, params, _);
},
//
// seeking all the row(s) by query values
//
function(params, count, cb) {
if (count > 0) {
// sqlSelect protocol :
// url, sort ording, sorting field, searching field, valid attributes and callback
util.sqlSelect(params, 'Cities', Models.City, validFields, 2, {}, undefined,
function(err, result) {
cb(err, {
"params": params,
"total_rows":count,
"data": _.map(result, function(item){ return item.selectedValues; })
});
});
}
else {
cb(null, {
params: params,
total_rows: count,
data: []
});
}
}
],
//
// final return
//
function(err, result) {
if (err) {
res.send(err.status, err.msg);
} else {
res.send(200, result);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment