From 8165beec55377b4045554d05225312e167b2e49f Mon Sep 17 00:00:00 2001 From: Chris Rhoden Date: Sun, 22 Jan 2012 19:27:58 -0500 Subject: [PATCH] untested but this should give you a starting point --- app.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index 40ab708..fef3b7f 100644 --- a/app.js +++ b/app.js @@ -563,15 +563,19 @@ app.get( '/schools', checkAjax, loadUser, function( req, res ) { // XXX mongoose's documentation on sort is extremely poor, tread carefully School.find( {} ).sort( 'name', '1' ).run( function( err, schools ) { if( schools ) { + // If schools are found, loop through them gathering any courses that are // associated with them and then render the page with that information. - sendJson(res, { 'user': user.sanitized, 'schools' : schools.map(function(school) { - var s = school.sanitized; - s['courses'] = Course.find( { 'school' : s._id } ).sort( 'name', '1' ).run(function( err, courses) { - return courses.map( function(c) { return c.sanitized; } ); - }); - return s - })}) + var schools_todo = schools.length; + schools.each(function (school) { + Course.find( { 'school': school.id } ).run(function (err, courses) { + school.courses_length = courses.length + schools_todo -= 1; + if (schools_todo <= 0) { + sendJson(res, { 'user': user.sanitized, 'schools': schools }); + } + }); + }); } else { // If no schools have been found, display none //res.render( 'schools', { 'schools' : [] } ); -- 2.25.1