react.rb icon indicating copy to clipboard operation
react.rb copied to clipboard

native param conversions

Open catmando opened this issue 9 years ago • 5 comments

what should happen if native objects are passed to react.rb components?

Currently we do nothing UNLESS you explicitly declare the type of the param, AND that type class has an associated react conversion method.

We've got lots of options here to discuss.

catmando avatar Feb 05 '16 19:02 catmando

@fkchang had this comment which triggered this issue:

I suspect this might be related to that issue u reported on the opal gitter -- but I'm wondering the right way to handle Hashes, and more particular nested data structures that have hashes in them. Since we write react.rb components in Ruby/Opal, I expect them to handle Hashes, the problem is that while I can always pass them Opal Hashes when called from opal, say parent passing in params, using react_component() for example, passes raw JS objects, where are not compatible. In some cases I preconvert JS objects, for example in a websocket handler, other cases I change the component to convert params, but all of those feel kind of hacky. Thoughts?

catmando avatar Feb 05 '16 19:02 catmando

Something to throw into this, that data structures nested > 1 level deep could be an issue. I wrote a converter to convert array of javascript objects into arrays of hashes, but I have data that's arrays of hashes, which might contain arrays of hashes etc. You get to where you need to know the data structure exactly. Maybe we could just do a JSON.parse of the string interpretation of the params or something

fkchang avatar Feb 05 '16 19:02 fkchang

Maybe if type is Native as in param :foo, type: Native it means to convert incoming to a string, then JSON parse it, and if that fails just wrap it in Native.

That way your param is always a valid ruby object.

catmando avatar Feb 05 '16 19:02 catmando

Hang on... thinking:

If you declare param :foo, type: Hash

and you pass a native object, then we should serialize-deserialize the object.

If you declare the type to be an array, then we check each element to see if it is a native object, and apply the above rule.

I believe everything else converts okay automatically.

Note that we already have an implementation that allows you to specify serializers for your own classes.. So if you declare the type to be some class, and that class has a convertor, and the incoming param is native object the convertor will be called. Reactive-Record uses this to convert from native objects back into active record models. The current way of specifying the conversion methods needs to be reworked (see #96)

catmando avatar Feb 05 '16 19:02 catmando

This issue was moved to reactrb/reactrb#127

sollycatprint avatar Jun 13 '16 13:06 sollycatprint