Skip to content
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

Complex object serialization changed in 1.1.0 #158

Closed
fabn opened this issue Aug 29, 2015 · 12 comments
Closed

Complex object serialization changed in 1.1.0 #158

fabn opened this issue Aug 29, 2015 · 12 comments

Comments

@fabn
Copy link

fabn commented Aug 29, 2015

I'm using the javascript geolocation api in an application and I'm passing the position object as a GET param into a Routes call. When I upgraded to 1.1.0 my code stopped working and the position object is not passed anymore. This is likely due to #155.

Here's my code (simplified)

navigator.geolocation.getCurrentPosition(function(position) {
  url = Routes.points_path({position: position.coords})
  // ...
})

On rails side using version 1.0.0 I have these parameters

"position"=>{"latitude"=>"xx.xxx", "longitude"=>"yy.yyyy", "altitude"=>"", "accuracy"=>"100", "altitudeAccuracy"=>"", "heading"=>"", "speed"=>""

In version 1.1.0 I get no params at all i.e. the generated url is /points.

It would be nice to have back the old behavior.

Thanks.

@le0pard
Copy link
Member

le0pard commented Aug 30, 2015

Just checked my app with js-routes 1.1.0:

> Routes.allReadApiV1InboxPath(1, {a: {b:2, c: 3}})
"/api/v1/inboxes/1/all_read?a%5Bb%5D=2&a%5Bc%5D=3"
> Routes.allReadApiV1InboxPath(1, {position: {latitude:50.34, longitude: 34.124}})
"/api/v1/inboxes/1/all_read?position%5Blatitude%5D=50.34&position%5Blongitude%5D=34.124"

Can you provide you js-routes js file with compiled routes?

@fabn
Copy link
Author

fabn commented Aug 30, 2015

I don't have it right now, I'll provide it later. Keep in mind that geolocation position is not a plain javascript object, can you try with the above code, i.e.

navigator.geolocation.getCurrentPosition(function(position) {
  Routes.allReadApiV1InboxPath(1, {position: position.coords})
})

Yesterday I spent some time debugging this and I think the key is the missing jQuery.param call in the new code. If I understood the new serializer it only serializes plain javascript objects while jQuery.param seems to handle position objects.

@le0pard
Copy link
Member

le0pard commented Aug 30, 2015

Hm, in this case if you have jquery, you can use:

JsRoutes.setup do |config|
  config.serializer = "jQuery.param"
end

@bogdan
Copy link
Collaborator

bogdan commented Sep 2, 2015

@fabn you should give us a more clear way to reproduce the issue. Maybe you pass a special type of JS object into route function that causes an issue. You need to figure it out by calling Routes.allReadApiV1InboxPath directly with manually built argument.

@le0pard
Copy link
Member

le0pard commented Sep 2, 2015

@fabn
Copy link
Author

fabn commented Sep 2, 2015

@le0pard Yes config serialize would solve my issue. I was wondering if it should be restored in gem code as default serializer. Otherwise this issue can be closed (maybe with a read me addition for that change)

@le0pard
Copy link
Member

le0pard commented Sep 2, 2015

@fabn if we set it as default, js will get error for scripts, where not present jQuery.

@fabn
Copy link
Author

fabn commented Sep 2, 2015

In the old code it was the default serializer if and only if jQuery is defined, imho it was a good approach.

@le0pard
Copy link
Member

le0pard commented Sep 2, 2015

@fabn As I understand this PR allow to use ANY serializer, not only jQuery or build-in. So, if you need jQuery serializer - you can use it by adding one line in config

@bogdan
Copy link
Collaborator

bogdan commented Sep 3, 2015

@fabn we found out that jQuery serialiser became incompatible to Rails serialiser that violates main js-routes principle to be rails compatible. That is why we removed that code from core. Use @le0pard's workaround to return jQuery-like behaviour.

@fabn
Copy link
Author

fabn commented Sep 3, 2015

Good to know, what incompatibility did you find?

In any case I think that this issue can be closed then.

@bogdan
Copy link
Collaborator

bogdan commented Sep 3, 2015

Incompatibility is described here: #153

@bogdan bogdan closed this as completed Sep 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants