function attachChild(output, child, depth)
{
    var ret = true;
    var childrenEntered = false; // set to true once we recurse into children
    for(var i in output)
    {
        if( child.parents.find(output[i]._id) )
        {
            child.depth = depth;

            output[i].children.push(child);

            // remove the parent we just attached
            child.parents = child.parents.exclude(output[i]._id);

            // only say success if we've attached to all the parents
            if( child.parents.length === 0 )
                return true;
        }
        if( output[i].children.length != 0 )
        {
            childrenEntered = true;
            ret = ret && attachChild(output[i].children, child, depth + 1);
        }
    }
    return ret && childrenEntered; // if we haven't looked at any children, ret will be incorrectly set to false which the && prevents
}
// returns true for success, false for failure.  This is used as part of buildDocumentHeirarchy
returns true for success, false for failure.  This is used as part of buildDocumentHeirarchy
returns true for success false for failure . This is used as part of buildDocumentHeirarchy
function attachChild(output, child, depth)
{
    var ret = true;
    var childrenEntered = false; 
    for(var i in output)
    {
        if( child.parents.find(output[i]._id) )
        {
            child.depth = depth;
            output[i].children.push(child);
            child.parents = child.parents.exclude(output[i]._id);
            if( child.parents.length === 0 )
                return true;
        }
        if( output[i].children.length != 0 )
        {
            childrenEntered = true;
            ret = ret && attachChild(output[i].children, child, depth + 1);
        }
    }
    return ret && childrenEntered; 
}
function attachChild ( output , child , depth ) { var ret = true ; var childrenEntered = false ; for ( var i in output ) { if ( child . parents . find ( output [ i ] . _id ) ) { child . depth = depth ; output [ i ] . children . push ( child ) ; child . parents = child . parents . exclude ( output [ i ] . _id ) ; if ( child . parents . length === 0 ) return true ; } if ( output [ i ] . children . length != 0 ) { childrenEntered = true ; ret = ret && attachChild ( output [ i ] . children , child , depth + 1 ) ; } } return ret && childrenEntered ; }
returns true for success, false for failure.
returns true for success false for failure .
// set to true once we recurse into children // remove the parent we just attached // only say success if we've attached to all the parents // if we haven't looked at any children, ret will be incorrectly set to false which the && prevents