Last active
August 29, 2015 14:06
-
-
Save geilt/97889b63b1951d40bb4e to your computer and use it in GitHub Desktop.
Combining Tables in Waterline with Promises.
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
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