-
Notifications
You must be signed in to change notification settings - Fork 3k
Repeated search parameters introduce unwanted polymorphism #1706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Another popular way annotating that a paramater is repeated is by using brackets after the name of the paramater followed by the value. For example: {
id: [1, 2]
} This annotation is used in the Ember router as well (http://emberjs.jsbin.com/ucanam/2849/edit) |
This seems to semi-work? |
Furthermore the This makes no sense to me. |
Specify
As for @georeith's point about undefined vs empty array, I think we made that choice to avoid a BC-BREAK. I agree though, if the param is explicitly specified an array type (either via |
I think we might be able to close this, given that |
what happens if you need the |
@juliancoleman |
@christopherthielen I'm passing a series of queries to my db, and I record them in the URL for the user to bookmark. When the user visits this bookmark, it will execute a query based on |
State parameters don't automatically work with arbitrary data structures. I think you have a few options:
If you use a query string, this would be something like
https://ui-router.github.io/docs/0.3.1/#/api/ui.router.util.$urlMatcherFactory#methods_type Here's a plunker with a custom param type that decodes to a series of arrays: |
@christopherthielen this is probably the most impressive answer I've received. I can't thank you enough for the time you spent to help me resolve this issue. That plunkr is exactly what I was looking for, but you also provided me with a means of declaring a JSON type as the URL parameter. |
Right now, if I want to pass multiple values into a search parameter, UI-router simply repeats the search parameter for every value that I pass in.
For example:
However, if I only pass in one user ID:
This is less-than-ideal, because it makes
$stateParams.id
polymorphic, even when I've specified a typed parameter, and requires a lot of type-checking in controllers.'At the very minimum, I end up with lots of:
Worse still, any state parameter risks being cast into an array if a user makes a typo, and specifies the same parameter twice. This seems likely to cause unpredictable breakages in controllers, given that most developers will not code defensively against this scenario.
IMO, the search parameter grouping introduced by #373 should require an explicit opt-in for each parameter (especially if the parameter is typed). If the matcher expects multiple values for a parameter, it should always deserialize the value(s) into an array, even if only one value is provided. If the matcher does not expect multiple values, it should return the first one that it finds, and discard subsequent matches (or throw an error).
Similarly, I'd also be interested to hear thoughts on introducing support for delimited search/query parameters (eg.
/url?id=1,2
). While UI-router's current implementation seems to be the most common, there does not appear to be a standard, and delimited lists arguably might produce more readable URLs...Certainly not appropriate for all use-cases, but possibly quite useful...
Summary
The text was updated successfully, but these errors were encountered: