Created
September 6, 2012 00:46
-
-
Save ciberch/3649019 to your computer and use it in GitHub Desktop.
Using distinct
This file contains hidden or 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
| var getDistinct = function (req, res, next, term, init){ | |
| var key = 'used.' + term; | |
| req[key] = init ? init : []; | |
| var query = {streams: req.session.desiredStream}; | |
| asmsDB.Activity.distinct(term, query, function(err, docs) { | |
| if (!err && docs) { | |
| _.each(docs, function(result){ | |
| req[key].push(result); | |
| }); | |
| next(); | |
| } else { | |
| next(new Error('Failed to fetch distinct ' + term)); | |
| } | |
| }); | |
| } | |
| //.. | |
| function getDistinctVerbs(req, res, next){ | |
| getDistinct(req, res, next, 'verb'); | |
| }; | |
| function getDistinctActors(req, res, next){ | |
| getDistinct(req, res, next, 'actor'); | |
| }; | |
| function getDistinctObjects(req, res, next){ | |
| getDistinct(req, res, next, 'object', ['none']); | |
| }; | |
| function getDistinctObjectTypes(req, res, next){ | |
| getDistinct(req, res, next, 'object.object.type', ['none']); | |
| }; | |
| function getDistinctActorObjectTypes(req, res, next){ | |
| getDistinct(req, res, next, 'actor.object.type', ['none']); | |
| }; | |
| //... | |
| app.get('/streams/:streamName', loadUser, getDistinctStreams, getDistinctVerbs, getDistinctObjects, getDistinctActors, | |
| getDistinctObjectTypes, getDistinctActorObjectTypes, getDistinctVerbs, getMetaData, function(req, res) { | |
| asmsClient.asmsDB.Activity.getStream(req.params.streamName, 20, function (err, docs) { | |
| var activities = []; | |
| if (!err && docs) { | |
| activities = docs; | |
| } | |
| req.streams[req.params.streamName].items = activities; | |
| var data = { | |
| currentUser: req.user, | |
| streams : req.streams, | |
| desiredStream : req.session.desiredStream, | |
| actorTypes: req.actorTypes, | |
| objectTypes : req.objectTypes, | |
| verbs: req.verbs, | |
| usedVerbs: req['used.verb'], | |
| usedObjects: req['used.object'], | |
| usedObjectTypes: req['used.object.type'], | |
| usedActorObjectTypes: req['used.actor.object.type'], | |
| usedActors: req['used.actor'] | |
| }; | |
| if (req.is('json')) { | |
| res.json(data); | |
| } else { | |
| res.render('index', data); | |
| } | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment