Skip to content

Instantly share code, notes, and snippets.

@geilt
Last active August 29, 2015 14:06
Show Gist options
  • Save geilt/97889b63b1951d40bb4e to your computer and use it in GitHub Desktop.
Save geilt/97889b63b1951d40bb4e to your computer and use it in GitHub Desktop.
Combining Tables in Waterline with Promises.
function findArgs(params, callback) {
params.args.sort = { key : 1, order: 0};
var type = '';
if(params.args.hasOwnProperty('type')) {
type = params.args.type;
delete params.args.type;
}
var finder = Meta.find(params.args)
.then( function(meta) {
return meta;
})
.fail( function(err){
if(callback && typeof callback === 'function') callback({err: err});
});
if(type !== 'all'){
finder.then( function(meta) {
if(callback && typeof callback === 'function') callback(meta);
});
return;
}
finder.then( function(meta){
var users = User.find({sort: 'lastName ASC'}).then( function(result) {
result.forEach(function(item, index){
result[index] = {id: item.id, key: 'users', value: item.username, label: item.fullName, description: item.displayName, order: 0};
});
return result;
});
return [meta, users];
})
.spread( function(meta, users){
var combined = meta.concat(users);
if(callback && typeof callback === 'function') callback(combined);
});
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment