make school slug page work
authorSeth Woodworth <seth@sethish.com>
Mon, 23 Jan 2012 04:53:00 +0000 (23:53 -0500)
committerSeth Woodworth <seth@sethish.com>
Mon, 23 Jan 2012 04:53:00 +0000 (23:53 -0500)
app.js
public/index.html
public/javascripts/main.js

diff --git a/app.js b/app.js
index da9ed2ca5f39410b475ad703d4977c45ee0286bc..6178a8d14981dacf42a3a964d6f57123be134d1f 100644 (file)
--- a/app.js
+++ b/app.js
@@ -357,20 +357,21 @@ function loadUser( req, res, next ) {
 // loadSchool is used to load a school by it's id
 function loadSchool( req, res, next ) {
   var user                     = req.user;
-  var schoolId = req.params.id;
+  var schoolName       = req.params.name;
   console.log( 'loading a school by id' );
 
-  School.findById( schoolId, function( err, school ) {
+  School.findOne({'name': schoolName}).run( function( err, school ) {
+    //sys.puts(school);
     if( school ) {
       req.school = school;
+      //req.school.authorized = authorized;
 
       // If a school is found, the user is checked to see if they are
       // authorized to see or interact with anything related to that
       // school.
-      school.authorize( user, function( authorized ){
-        req.school.authorized = authorized;
-        next();
-      });
+      //school.authorize( user, function( authorized ){
+      //});
+      next();
     } else {
       // If no school is found, display an appropriate error.
       sendJson(res,  {status: 'not_found', message: 'Invalid school specified!'} );
@@ -567,26 +568,24 @@ app.get( '/schools', checkAjax, loadUser, function( req, res ) {
       // associated with them and then render the page with that information.
       var schools_todo = schools.length;
       schools.map(function (school) {
-        sys.puts("making course query");
-       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.map( function(s) {
-                        sys.puts("here");
-                        var school = {
-                            _id: s._id,
-                            name: s.name,
-                            description: s.description,
-                            url: s.url,
-                            slug: s.slug,
-                            courses: s.courses_length
-                        };
-                        return 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.map( function(s) {
+                var school = {
+                  _id: s._id,
+                  name: s.name,
+                  description: s.description,
+                  url: s.url,
+                  slug: s.slug,
+                  courses: s.courses_length
+                };
+                return school;
+              })
+            });
+          }
+        });
       });
     } else {
       // If no schools have been found, display none
@@ -596,35 +595,46 @@ app.get( '/schools', checkAjax, loadUser, function( req, res ) {
   });
 });
 
-app.get( '/school/:slug', checkAjax, loadUser, loadSchoolSlug, function( req, res ) {
+app.get( '/school/:name', checkAjax, loadUser, loadSchool, function( req, res ) {
   var school = req.school;
   var user = req.user;
-  console.log( 'loading a school by school/:id now slug' );
+  var courses;
+  console.log( 'loading a school by school/:id now name' );
 
-  school.authorize( user, function( authorized ) {
+  //school.authorize( user, function( authorized ) {
     // This is used to display interface elements for those users
     // that are are allowed to see th)m, for instance a 'New Course' button.
-    var sanitizedSchool = school.sanitized;
-    sanitizedSchool.authorized = authorized;
+    //var sanitizedSchool = school.sanitized;
+    var sanitizedSchool = {
+      _id: school.id,
+      name: school.name,
+      description: school.description,
+      url: school.url
+    };
+    //sanitizedSchool.authorized = authorized;
     // Find all courses for school by it's id and sort by name
     Course.find( { 'school' : school._id } ).sort( 'name', '1' ).run( function( err, courses ) {
       // If any courses are found, set them to the appropriate school, otherwise
       // leave empty.
+      sys.puts(courses);
       if( courses.length > 0 ) {
-        sanitizedSchool.courses = courses.filter(function(course) {
+        courses = courses.filter(function(course) {
           if (!course.deleted) return course;
         }).map(function(course) {
           return course.sanitized;
         });
       } else {
-        sanitizedSchool.courses = [];
+        school.courses = [];
       }
+      sanitizedSchool.courses = courses;
+      sys.puts(courses);
+
       // This tells async (the module) that each iteration of forEach is
       // done and will continue to call the rest until they have all been
       // completed, at which time the last function below will be called.
       sendJson(res, { 'school': sanitizedSchool, 'user': user.sanitized })
     });
-  });
+  //});
 });
 
 // FIXME: version of the same using school slugs instead of ids
index e964aa53f33a340c7ed0c2330a6a5a4895405dd7..2212c26df180069d9d06e2ecd29ce32a1000b8f6 100644 (file)
 
                 <script id="schoolTmpl" type="text/x-jquery-tmpl">
                     <li>
-                        <a href="school/${_id}" class="school">
+                        <a href="school/${name}" class="school">
                             <img class="icon-pair" src="/assets/img/avatar-univerisity-generic.gif" alt="" />
                             <div class="text-pair">
                                 <span class="school_name">${name}</span>
                     </header>
 
                     <div class="main-content">
+                        <div id="courselist">
+                        </div>
 
                         <span class="sub_menu" style="display:none;"></span>
 
+                        <script id="courseTmpl" type="text/x-jquery-tmpl">
+                        <div class="course">
+                            <a class="course-name" href="/course/${_id}">
+                                ${number}: ${name}
+                            </a>
+                            <p class="course-description">
+                                ${description}
+                            </p>
+                            <div class="dept">${department}</div>
+                            <aside class="course-data">
+                                <div class="course-data-item">
+                                    <div class="course-data-number">${lectures}</div>
+                                    <div class="course-data-description">lectures</div>
+                                </div>
+                            </aside>
+                        </div> <!-- .course -->
+                        </script>
+                        <!--
                         <div class="proto course" id=PROTO_course>
                             <div class="course-content">
                                 <a class="course-name" href="/course/___id__">__number__: __name__</a>
                                 </div>
                             </aside>
                         </div>
+                        -->
 
                     </div>
                                                                                <div class="sidebar">
index 498ff46ceb3a6622860bb22d601892539185a79c..bf69e6880e2975596877757b814c3e3e4602104e 100644 (file)
@@ -145,8 +145,8 @@ router.add('schools', function(data, cb) {
   }
 
   $('#pg_schools').fadeIn(function() {
-               $('#g-footer').fadeIn();
-       });
+      $('#g-footer').fadeIn();
+  });
   $('#schoolTmpl').tmpl( data.schools ).appendTo("#pg_schools #schools");
 });
 
@@ -156,11 +156,15 @@ router.add('school', function(data, cb) {
   $('.sub_menu').hide();
   //$('#new_course').unbind();
   $('#form_course').hide().unbind();
-  var response = {
+
+  response = {
     id: 'course',
     data: data.school.courses
   }
 
+  $('#pg_courses').fadeIn(function() {
+      $('#g-footer').fadeIn();
+  });
   $("#school_name").html(data.school.name);
 
   if (data.school.authorized) {
@@ -183,7 +187,8 @@ router.add('school', function(data, cb) {
       });
     })
   }
-  cb("courses", response)
+
+  $('#courseTmpl').tmpl( data.school.courses ).appendTo("#pg_courses #courselist");
 });
 
 // go to the page that lists the lectures for a specific course