1

I am using Ember.js in my application. In a .jsp page I have a button and when I click on the button I call the url = "emberapp/myproject/routename?isFromJSP=true&data=[1,2]".

This correctly routes to my routename.

The problem is, I want to read the boolean parameter (isFromJSP) and list parameter (data) in my controller or route. How to do so?

PS:I know to use window.location.href but it is not returning the current page in my EMBER init function. I am using AJAX calls to get data for my ember page in the init function and I want to pass these request parameters as request json objects.

CODER
  • 13
  • 5
  • Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Tijmen Sep 18 '17 at 14:41
  • Possible duplicate question. https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – Thusitha Sep 18 '17 at 14:43
  • its not a duplicate. Read the question carefully. When i use window.location.href in my ember page, its returning previous page's url. – CODER Sep 18 '17 at 15:23

1 Answers1

1

You should checkout the query params.

Basically you can do this in your controller:

queryParams: ['isFromJSP', 'data'],
isFromJSP: false,
data: [],

Next you can either use this for computed properties, or access them on the params hash passed to the routes model hook. If you do the later consider to add this to your route:

queryParams: {
  isFromJSP: {
    refreshModel: true
  },
  data: {
    refreshModel: true
  }
},

this will enforce that a change to the query params will reload the model (and will do a full transition).

Lux
  • 17,835
  • 5
  • 43
  • 73
  • If i am using two ember pages to pass parameters, then i can use this query params. But my scenario is different. I cant use the queryparams as i m transferring the control from a .jsp page. – CODER Sep 18 '17 at 16:38
  • how can i access them in my controller? I cant get the way you specified in the code. Could you give me an example? – CODER Sep 18 '17 at 16:43
  • in my params map, i am not getting these passed values. – CODER Sep 18 '17 at 16:45
  • Then something is wrong. This should work. [See this twiddle](https://ember-twiddle.com/f547f9e7bba972a92b11eae5b37348ca?openFiles=templates.application.hbs%2C), just enter something like `/?data=[1,2]` to the "virtual" url. – Lux Sep 18 '17 at 16:48
  • Thanks a lot. I got it! One more doubt. When i reload my ember page, my route is changed twice and the url is changed like "#/emberapp/checkQuery/checkQuery/function". But if i use the function transitionTo() the url is maintained as expected "#/emberapp/checkQuery/function". Could you guide me here?? – CODER Sep 18 '17 at 16:57
  • you do something wrong. But I notice the `#` in the beginning. Are you using hash locations? Because then you have to use hash locations for the link as well. – Lux Sep 18 '17 at 17:03
  • Thanks for the help! The thing is, when I use query params, it is set to default for the entire session. I want it to change periodically. Is it compulsory to send them in all routes? Otherwise will it take the previous value as default value/ – CODER Sep 19 '17 at 07:15
  • I don't really understand what you want. If you want to discuss this, maybe you should checkout the ember community slack help channel – Lux Sep 19 '17 at 07:33