To show a single repository, I added more attributes to the Dashboard.Repository model.
A specific repository shall be accessible via the route /:username/:repository. Therefore, I updated the router so the user route now contains 2 sub-routes index and repository. Inside the index route everything is setup as before: get the watched repositories for the user and connect the outlet. The repository route gets the repository which is specified in the url and connects the outlet. That simple. Because both sub-routes need to know what the ‘current’ username is, I’ve created a Dashboard.UserController which currently just holds the username. The value of this property is set in the connectOutlets of the user route. The updated user route now looks like this:
user:Ember.Route.extend({route:'/:username',connectOutlets:function(router,context){router.set('userController.username',context.username);},index:Ember.Route.extend({route:'/',connectOutlets:function(router){varusername=router.get('userController.username');varstore=router.get('store');// get watched repositories for given usernamevarwatchedRepositories=store.findQuery(Dashboard.Repository,{username:username,type:'watched'});// set watched repositories on repositoriesControllerrouter.set('repositoriesController.content',watchedRepositories);// show repositoriesrouter.get('applicationController').connectOutlet('repositories');}}),repository:Ember.Route.extend({route:'/:repository',connectOutlets:function(router,context){varusername=router.get('userController.username');varrepoName=context.repository;// fetch repo for current user// id of the repository is username/repoNamevarrepo=router.get('store').find(Dashboard.Repository,'%@/%@'.fmt(username,repoName));router.set('repositoryController.content',repo);// show repositoryrouter.get('applicationController').connectOutlet('repository');}})})
To get a specific repository I implemented the find method on GitHubAdapter as follows:
To show watched repositories of a user, I added the action showUser to the router. The action showRepository handles the case when a specific repository shall be shown. These actions are declared inside the user route:
app/lib/router.js
12345678910111213141516171819202122232425
showUser:function(router,evt){varusername;varcontext=evt.context;// context is a Dashboard.RepositoryController if this action// is called from repository template --> this needs to be fixedif(Dashboard.RepositoryController.detectInstance(context)){username=context.get('owner.login');}else{username=context;}router.transitionTo('user.index',{username:username});},showRepository:function(router,evt){// context is the full_name of a repository: username/repositoryvarsplit=evt.context.split('/'),username=split[0],repository=split[1];router.transitionTo('user.repository',{username:username},{repository:repository});}
As a final step, I adapted the repositories template so it’s possible to show a specific user or repository: