I'm switching to the new ember router but have a very simple question -- how do I figure out what route I'm currently in? Before you could do something like App.router.get('currentState')
, but this doesn't seem to work anymore as the router no longer inherits from StateManager
Have a look at this question.
Summary: the currentState
is now stored in the property currentPath
in your ApplicationController. The accepted solution was to observer this property to write it into a global property:
App = Em.Application.create({
currentPath: ''
});
ApplicationController : Ember.Controller.extend({
updateCurrentPath: function() {
App.set('currentPath', this.get('currentPath'));
}.observes('currentPath')
});
I will probably get blasted for this but after a ton of frustration with this I decided to implement a really ugly workaround.
My use case was trying to get the ID of the current post because I was posting a reply to it. Think something like this for our current route:
"...#/post/12345"
my workaround (ugliest code ever):
var currentId = window.location.hash.split('/')[2];
App.Message.createRecord({
content: message,
inReplyTo: currentId
}).get('transaction').commit();
In controller I could get the current route name using the code below. Good luck
this.get("currentRouteName")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With