22

In 1.7 Ember should support Query Parameters. I have no problems using them in controller but I'd like to access them in Route, ideally in beforeModel hook but model hook would work as well.

The API docs mention a queryParam parameter for the beforeModel hook but if I try to use it, it is always undefined.

The Query Parameters guide seems to suggest that the query parameters should be accessible as a part of the first parameter to the model hook. But that is also undefined. See the code below for examples.

Is there a way to access the query parameters from Route?

Thank you for your help.

App.ApplicationRoute = Em.Route.extend({
   beforeModel: function(transition, queryParams){
       console.log(queryParams.test); //undefined at /?test=123
   },
   model: function(params){
       console.log(params.test); //undefined at /?test=123
   }
}); 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jancervinka
  • 247
  • 1
  • 2
  • 7
  • 2
    You should be able to pull it off of the `params` object in the model hook. see: http://emberjs.jsbin.com/necid/1/edit?html,js,console,output Are you setting `test` as a queryParam in the controller? – tikotzky Sep 19 '14 at 22:34
  • Thank you, that was it. It never occurred to me that Controller could affect Route. I thought that the setting was only for accessing queryParams directly in the Controller. – jancervinka Sep 20 '14 at 15:26

5 Answers5

24

Pretty sure it's a bug, but you can access them in the meantime via the transition object:

App.ApplicationRoute = Em.Route.extend({
   beforeModel: function(transition){
       console.log(transition.queryParams.test);
   }
}
typeoneerror
  • 55,990
  • 32
  • 132
  • 223
  • 2
    Thank you, this does work and it's cleaner to do this in beforeModel hook. It's strange that even in 1.8.1 it still hasn't been fixed. I'm not sure if it's a bug or just an unclear documentation. – jancervinka Nov 05 '14 at 17:06
  • @typeoneerror: Your solution only works if the locationType is auto. Do you know how to grab query params if the location type is hash – iOS dev Mar 22 '17 at 21:35
8

By specifying the query params in the controller, params will contain them automatically

ApplicationController = Ember.Controller.extend({
  queryParams: ['test'],
  test: null
});
Ramy Ben Aroya
  • 2,333
  • 14
  • 20
7

In the latest version of ember (2.12 at the time of writing this answer), queryParams can be accessed in the model hook as follows:

import Ember from 'ember';

export default Ember.Route.extend({
    queryParams: {
        test: ''
    },
    model(params) {
        console.log(params.test);
    },
});

Observe that now both dynamic segment and queryParams are accessible via the params object. Since params is not available in the beforeModel hook, this solution works on when you have to access the queryParams in the model hook.

vantony
  • 513
  • 6
  • 9
5

In latest ember version you can get the value in Route as

import Ember from 'ember';
export default Ember.Route.extend({
  beforeModel(params){ 
      console.log(params.queryParams.test); //if the parameter contains test as the key
  }
});

This works only if the locationType is auto in your environment.js file.

iOS dev
  • 470
  • 7
  • 23
-4

If you want to access in didTransition action,

didTransition: (queryParams) ->
  console.log(@get('queryParams'))
Dhara Joshi
  • 407
  • 5
  • 9