2
votes

I'm trying to get a param from the URL, but it appears it doesn't get assigned at all. I was following this guide http://emberjs.com/guides/routing/query-params/ Below is the index.js controller.

export default Ember.Controller.extend({

  queryParams: ['authToken'],
  authToken: null,

  init: function() {
    var authToken = this.authToken;
    console.log(authToken);
  }
});

When accessing the root URL / or /#, authToken is null, which works as expected. However, when trying out /#?authToken=123, it's still null. Any ideas ?

1

1 Answers

7
votes

Well, I couldn't get value of authToken inside init hook - I think it's called too early, but you can wrap this in Ember.run.next method or get its value in setupController hook in IndexRoute. This works as expected:

App.IndexRoute = Ember.Route.extend({
  setupController: function (controller, model) {
    console.log(controller.get('authToken'));
  }
});

App.IndexController = Ember.Controller.extend ({
  queryParams: ['authToken'],
  authToken: null,
  init: function() {
    this._super();
    Ember.run.next(this, function() {
      console.log(this.get('authToken'));
    });

  }
});

For example, URL site.com#/?authToken=lol produces following console output:

app:49 lol
app:59 lol

Working demo.