couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tane Piper <piper.t...@gmail.com>
Subject Re: Including values from linked documents
Date Mon, 18 Oct 2010 19:05:51 GMT
On Mon, Oct 18, 2010 at 4:41 PM, Tane Piper <piper.tane@gmail.com> wrote:
> Hi there,
>
> I have a question about views - I've thrown up the code into this
> gist: https://gist.github.com/4cd88fc6a5aac5467774
>
> What I have is a ExtJS grid that I load these assets in to, each one
> looks like this document and it loads fine - but of course in the
> columns for the three fields location (docType Office), owner and
> delegate (both docType Role) it displays the ID rather than the name.
>
> Is there a way, without having to go out and fetch these documents
> with further requests, that I can include the name from each document
> in this view?
>
> Thanks in advance
>
> - Tane Piper
>

Ok, I think I managed to figure this out, so I thought I would share
it here and hopefully someone can sanity check it as well in case
there is a better way to do this:

My map function now looks like this:

function(doc) {
    if (doc.docType == 'Asset') {
        emit([doc._id, 'asset'], {_id: doc._id});
        if (doc.location) {
            emit([doc.location, 'location'], {_id: doc.location});
        }
        if (doc.owner) {
            emit([doc.owner, 'owner'], {_id: doc.owner});
        }
        if (doc.delegate) {
            emit([doc.delegate, 'delegate'], {_id: doc.delegate});
        }
     }
}

That produces an output of:

{"total_rows":8,"offset":0,"rows":[
{"id":"89dd7ff6553c8cd2d44489c4f304bf33","key":["89dd7ff6553c8cd2d44489c4f3033d05","location"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f3033d05"}},
{"id":"89dd7ff6553c8cd2d44489c4f304c93a","key":["89dd7ff6553c8cd2d44489c4f3033d05","location"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f3033d05"}},
{"id":"89dd7ff6553c8cd2d44489c4f304bf33","key":["89dd7ff6553c8cd2d44489c4f3039337","owner"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f3039337"}},
{"id":"89dd7ff6553c8cd2d44489c4f304bf33","key":["89dd7ff6553c8cd2d44489c4f303f04f","delegate"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f303f04f"}},
{"id":"89dd7ff6553c8cd2d44489c4f304c93a","key":["89dd7ff6553c8cd2d44489c4f303f04f","owner"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f303f04f"}},
{"id":"89dd7ff6553c8cd2d44489c4f304bf33","key":["89dd7ff6553c8cd2d44489c4f304bf33","asset"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f304bf33"}},
{"id":"89dd7ff6553c8cd2d44489c4f304c93a","key":["89dd7ff6553c8cd2d44489c4f304c93a","asset"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f304c93a"}},
{"id":"89dd7ff6553c8cd2d44489c4f304c93a","key":["89dd7ff6553c8cd2d44489c4f304cadd","delegate"],"value":{"_id":"89dd7ff6553c8cd2d44489c4f304cadd"}}
]}

Then in my code, I need to loop over it twice to produce the correct
output for my JSON:

var assets = [];
data.rows.forEach(function(row) {
    if (row.key[1] == 'asset') {
        var asset = row.doc;
        data.rows.forEach(function(row) {
            if (row.id == asset._id && row.key[1] == 'location')
asset.location_display = row.doc.name;
            if (row.id == asset._id && row.key[1] == 'owner')
asset.owner_display = row.doc.name;
            if (row.id == asset._id && row.key[1] == 'delegate')
asset.delegate_display = row.doc.name;

        });
        assets.push(asset);
    }
});

Mime
View raw message