Goal:
Attach all the necessary JavaScript and CSS files to the page, when a rendered entity is delivered on a Web Socket event.
Given:
- I have a Web Socket with an event when a new entity is created (Via pusher.com but nevermind).
- The Web Socket delivers JSON with some meta-data(entity id, created date, title, etc.) and HTML of a single rendered entity.
- I catch the event in and render the entity (I do it in JavaScript, client is the same Drupal website).
Problem:
JavaScript and CSS files, needed for rendering, are not attached on the page. (E.g. scripts for display of an image gallery, lazy-loading of images).
My ideas:
I think that I should list all the necessary JavaScript and CSS files (or something like ajax commands, which can attach assets) in the JSON, when I push the event. To achieve this I might need to rendered the entity via some Drupal-AJAX-Renderer.
Afterwards (when I receive the JSON from Web Socket) there should be some way on a Drupal website to attach these files by executing some JavaScript method in Drupal.ajax.
Currently I haven't figured out how to achieve this. Could you give some advice?