trivago / melody

Melody is a library for building JavaScript web applications.
https://melody.js.org
Apache License 2.0
215 stars 39 forks source link

Q: Is it possible to carry over the data provided to twig template in melody? #59

Open st-schneider opened 5 years ago

st-schneider commented 5 years ago

Is it possible that, if I have product information for example, that gets fed into the twig template and expected as a 'product' variable in twig, to be available in melody as well?

pago commented 5 years ago

Not sure I understand the question. Would you like Melody to pick up on data that you've rendered on the server side using Twig/PHP automatically?

That unfortunately won't work. You'd need to serialize the data and pass it to the client side in another way, for example by doing <script>INITIAL_STATE = {{ _context | json_encode() }}</script> (making sure this is only done in a template rendered on the server side).

When rendering the template on the client side using the same data as on the server side, Melody will however be smart enough to keep the existing DOM nodes in place.

st-schneider commented 5 years ago

Is there any way to inject the context into a store automatically maybe? Where can I inject the context for my component? Only when the render function is called or when I assemble the component? There seem to be some white spots in the documentation for this :)

st-schneider commented 5 years ago

Wouldn't the storaging of the whole context go against the information hiding principle? Just thinking about DSGVO

pago commented 5 years ago

We usually use redux to maintain a global state with components connecting to it (via melody-redux). Not sure how the DSGVO fits here since you'd just be transporting state from backend to frontend but wouldn't move it out of the users control.