untested but this should give you a starting point
authorChris Rhoden <carhoden@gmail.com>
Mon, 23 Jan 2012 00:27:58 +0000 (19:27 -0500)
committerChris Rhoden <carhoden@gmail.com>
Mon, 23 Jan 2012 00:27:58 +0000 (19:27 -0500)
app.js

diff --git a/app.js b/app.js
index 40ab7087651ec6b8f2317b53e74f5802e047eef7..fef3b7f78d5bafe0967c251ed01fa64644b30026 100644 (file)
--- 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' : [] } );