Search code examples
ember.jsember-router

Optional route (URL part)


We are developing educational platform. We have two resource types: courses and lessons. So we do have following URL schema:

/(root)
└┬course-slug
 ├─info
 └┬lesson-slug
  └┬comments
   └─comment-id

URL to a lesson looks like this: /programming/what-is-loop/ and comments for this lesson would have following URL: /programming/what-is-loop/comments/.

Now we decided that course is optional. Some lessons are not inside any course, some inside many courses. So new URL schema looks like this:

/(root)
├┬course-slug
│├─info
│└┬lesson-slug
│ └┬comments
│  └─comment-id
│
└┬lesson-slug
 └┬comments
  └─comment-id

In other words course part of URL are optional. How to make CourseRoute optional, or how to reuse LessonRoute, CommentsRoute and CommentRoute (in fact there is little more)


Solution

  • Thx to all!

    It turned out that easiest way is to move course-slug to query-params. Just few line of code needed to achive this:

    App.LessonRoute = Em.Route.extend
      setupController: (controller, model, transition)->
        controller.set 'content', model
    
        course_slug = transition.queryParams.scope
        if course_slug
          ...
          @get('store').find('course', course_id).then (course)->
            controller.set 'course', course
    
    App.LessonController = Em.ObjectController.extend
      scope: null # slug for course
      queryParams: ['scope']
    

    Because course property simply moved from lesson-model to controller no heavy refactoring needed in templates or somewhere else.