rjaros / kvision

Object oriented web framework for Kotlin/JS
https://kvision.io
MIT License
1.24k stars 67 forks source link

How do I clone a component? #551

Open RORAKUS opened 4 days ago

RORAKUS commented 4 days ago

How do I clone a KVision component? I'm trying to make a library with a function, where you get a template Component object and it will add it multiple times to a container, each with different attribute values. Cloning would be extremely useful - or is there other way?

I tried JSON serialize/deserialize. Didn't work. Creating my own clone? Not possible - no way to get all attributes so I can clone them.

Thanks!

rjaros commented 4 days ago

I don't think it is possible. I'm not sure what's your use case, but I think you have some function which creates this component. What is the problem to just call this function multiple times?

RORAKUS commented 4 days ago

@rjaros It doesn't create a component. It receives one as a parameter. I'm making a device specific library, where you have a deviceSpecific{} wrapper, where you can define attribute values for different screen sizes. The point is so you don't have to write the component over and over just to change a small detail. In this wrapper you can normally add components, but the component will be duplicated with proper attributes and display values. Is there any way to duplicate it some shady javascript/kotlin reflection magic way?

rjaros commented 4 days ago

Almost all KVision components are also containers for some other components, so even duplicating all properties and attributes would not be enough. We would have to duplicate whole subtrees of components and rewrite all the references from children to their parents. And there is no way to do this with some general method in some top level component. It would be really a lot of work.

I have the impression that your problem would be easier to solve with a more functional approach, but I don't know how would it impact your whole project.

RORAKUS commented 4 days ago

Do you think the work would be worth it? I think it should! A proper framework like this should surely have a method to copy/clone objects! I'm also missing a direct way to recall all component classes and attributes. It would be useful too. I've looked into the code and it sure looks hard, but possible.

rjaros commented 4 days ago

I'll try to play a bit with this when I have some spare time. But of course PR is welcomed.

RORAKUS commented 2 days ago

I've successfully implemented what I wanted using after insert hook to duplicate the element after inserting :). Thanks for fast replies and help!