concatRes(addAttrReference, fnsPesquisa [, callback])
param
type
description
addAttrReference
boolean
true se quer que adicione um atributo dizendo de que model ela é
fnsPesquisa
array
array de objeto com controller e query
callback
função
opcional, função de retorno quando terminar a concatenação
var cFis = {
name : ( ) => 'fisica' ,
pesquisa : ( query , cb ) => {
cb ( [ { queryUsada : query } ] ) ;
}
} , cJu = {
name : ( ) => 'juridica' ,
pesquisa : ( query , cb ) => {
cb ( [ { queryUsada : query } ] ) ;
}
} ;
function concatRes ( addAttrReference , fnsPesquisa , callback ) {
let arrayRetorno = [ ] ;
chamaProximoController ( 0 ) ;
function chamaProximoController ( index ) {
if ( index < fnsPesquisa . length ) {
fnsPesquisa [ index ] . controller . pesquisa ( fnsPesquisa [ index ] . query , ( resultado ) => {
if ( addAttrReference === true ) {
resultado = resultado . map ( ( res ) => {
return { ...res , nameCollection : fnsPesquisa [ index ] . controller . name ( ) } ;
} ) ;
}
arrayRetorno = [ ...arrayRetorno , ...resultado ] ;
chamaProximoController ( index + 1 ) ;
} ) ;
} else {
// terminou tudo, manda o array concatenado
callback ( arrayRetorno ) ;
}
}
}
concatRes ( true ,
[ { controller : cFis , query : 'Paulão' } ,
{ controller : cJu , query : 'Maquinas do Serginho' } ] , function ( array ) {
console . log ( array ) ;
} ) ;