Query Params Casting for null


#1

Hi all, I’m wondering if there’s a solution to change the casting of query params. I like that they are casted to the same type as their default values, but I’m wondering if the same could be done for default values of null. I’d like my query params to be of type Integer even when defaulted to null.

For example, if I have the following in my controller:

queryParams: ['number'],

number: computed({
  get() { return null; },
  set(key, value) { return parseInt(value) || null; }
})

I’d like that once number is set, params.number at the route level would give me an Integer, not String. Currently this is only possible if I set the default value to an Integer, but I want null.

Any ideas?


#2

Short answer, don’t use computed property for query params.


#3

I don’t think CP is the culprit here though - if I just set it as null it will still cast as String. The problem is that the casting can’t be customized for nulls. I could set it as 0 and then change the value back to null on init, but since I have refreshModel set to true at the route level that wouldn’t be ideal for me. For now I’m simply casting the value back to Integer whenever I need it at the route level.


#4

I don’t remember where exactly but I do remember couple of days ago I read a long discussion on Github about some related problems caused by CP based query params, and the final answer is, query params are far more than a normal property, so the simplest and best way is to avoid define them as CPs.

If this is really important to you, I suggest you to find this issue on ember/ember-data in Github.

UPDATE:

FYI, this is not the issue I mentioned before but similar. This may help you to solve your problem.


#5

This is very helpful. Thanks @nightire!