-
Notifications
You must be signed in to change notification settings - Fork 148
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
Removing URL params with undefined or null values #153
Comments
js-routes's serialization was made compatible with ones provided by jQuery and Rails.
We don't aim for fixing word wide accepted spec here. |
The thing I can propose is to make |
You are totally right that It seems like one of the primary purposes of js-routes is to provide javascript route helpers with the same functionality as the "named route helpers" in Rails. Let's assume we have the following Rails route definition:
Rails gives us this nice
which corresponds nicely to the following
This is great! It's really cool to have route helpers that work the same way on both the server and the client side of my application. Now let's take a look at what happens when I use the same helpers with parameters which may be Rails actually excludes keys with
while js-routes leaves the key in the string and gives it an empty value:
So, js-routes is inconsistent with the behavior of the named route helpers in rails, which I believe is one of the primary goals of js-routes. Now, Rails does use I noticed that you are actually using It would be really great if we could get the js-routes helpers to function in line with the rails helpers. I propose simply removing the call to |
Yes, now I see your point. Thanks for explaining that rails url generation has some logic on top of It is a little fail that jQuery presence changes behaviour. Especially with a fact that we have code that we have a test: expect(evaljs("Routes.inboxes_path({uri: null, key: undefined})")).to eq(routes.inboxes_path(:uri => nil, :key => nil)) That will fail when jQuery will be in place. There is a reason why So the solutionI propose to do 3 things:
JsRoutes.configure { |c|
c.serialize = "jQuery.param"
//or
c.serialize = "function (params) { ... }"
//or
c.serialize = "MyAppNamespace.serialize_params"
} In this way people should be able to get previous behaviour any behaviour they want.
|
I like number 1 idea - make configurable serializer. |
I totally understand the desire to use I think users may want one of the following options:
As you said earlier, serialization is not a super easy problem, so it's likely that any user-provided custom serializer will have a lot of bugs. I'm not sure that opening the door to a custom serializer is really a win for users, since they are most likely to want one of the other two options above. (I really think users will most likely want option 2, but I'm clearly a little biased, as it's also what I want). |
Implementation of striping nulls before calling Configurable serializer can be handy when you are using other Ajax library than jQuery. I am 100% each library that provides ajax has its own serialize. I am not sure why it wasn't asked before to support others than jQuery. I dont' believe people should implement it from scratch: not more than a little custom code on default options for existing one. |
Okay, a configurable serializer definitely makes more sense when you want to avoid maintaining the serialization code, and want to support multiple ajax libs. But if we do pursue that option, I have one more question: how would we allow users to configure it? The custom serializer would naturally need to be written in javascript, but all of the js-routes config code is written in ruby. This smells a little bit to me, but that's probably the simplest thing to do. Something like this? JsRoutes.setup do |config|
config.serializer = "myApp.myCustomSerializerFn"
end And I suppose we would just default it to |
Yes, configuration looks right. We don't expect people to write something like: config.serializer = "function (params) { /* a lot of code here */ }" Only newbie developer would do it and this option is only for pro.
So I propose use our built-in by default that will be indicated as |
Okay, that sounds good to me. I'll put together a pull request and send it over. |
Allow Configurable Serializer Function (Solves #153)
I'm using this library all over the place in a very large application, and most of those places where I'm passing state objects as params to
Route.*_path()
functions. There are many cases where some of object properties areundefined
ornull
at various times ending up with query strings which look like this:It would be really great if these functions automatically filtered out
null
orundefined
values before building the query strings.The nature of this library is that there are a ton of different, but very similar functions which end up getting called all over the place. Because of this, there isn't really a path to fix this kind of issue in my application code without writing code to filter these values in every single place we call one of these functions.
Surely the vast majority of users are not interested in params with empty values ending up in url query strings, especially, when you are working with many keys which may potentially be empty.
I will be happy to submit a pull request to have this fixed if you will merge it.
Thanks
The text was updated successfully, but these errors were encountered: