What is the correct way to get the current path with slug/id from a controller?


#1

I’m sure I’m doing everything incorrect here so your guidance is much appreciated. By the way, I’m using ember-cli with ember-data, and even though I’ve built a few Ember projects I still consider myself pretty novice.

Question #1: It seems like in my ‘categories/category.js’ controller using { needs: [‘application’] } I can trace in Chrome the ‘controllers.application’ object and see a ‘currentPath’ property right there in Chrome dev tools, but then when I trace ‘controllers.application.currentPath’ it returns as ‘undefined’ ??!

I’m trying to get the current page so that I can update a menu dropdown, and so in my ‘category’ controller I’m defining this:

onPageChange:function(){
 console.log("Page Change", this.get('controllers.application.currentPath'));  // returns 'Page Change undefined'

 console.log("Page Change", this.get('controllers.application'));  // returns a Class object that has both 'currentPath' and 'currentRouteName' (shows as 'categories.category'

}.observes('currentPath')  // why does this only update if I have 'currentPath' observed, and not 'controllers.application.currentPath'??

Question #2: Say I’m able to grab the currentPath, why doesn’t it show the full path with the selected slug? It just shows ‘categories.category’ and not ‘categories.category.my-sub-page’? My routes are:

this.resource('categories', {path:'/'}, function(){
		this.route('category', {path: ':category_slug'});
	});

#2

So, I powered through to a solution, but I’m sure there’s a more “Ember” way to achieve this, which would be great if someone would like to share.

// controllers/categories/category.js

...

categoryCollection:{}, // collection of the categories, which are the slugs used    
onRouteChange:function(){
    		this.set('selectedCategory', this.get('categoryCollection')[this.get('model').category_slug]);  // I'm setting the 'selectedCategory' variable that updates a dropdown, which has all of the parent categories as options
    	}.observes('currentPath'),

#3

Here is something I threw together to help someone here with a similar question.

http://jsfiddle.net/NQKvy/1191/

There could be a better way, I’m not sure.