Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js get current route

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

like image 704
Sherwin Yu Avatar asked Apr 10 '13 09:04

Sherwin Yu


3 Answers

Have a look at this question.

Summary: the currentState is now stored in the property currentPathin 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')
});
like image 139
mavilein Avatar answered Oct 05 '22 17:10

mavilein


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();
like image 22
andreimpop Avatar answered Oct 05 '22 19:10

andreimpop


In controller I could get the current route name using the code below. Good luck

this.get("currentRouteName")

like image 22
Recep YALMAN Avatar answered Oct 05 '22 19:10

Recep YALMAN