My website has different elements called 'widgets'. Each of these widgets consists on an isolated component, which is basically a div
and a JSON object for it. The data for each widget returns from PHP in JSON format, I then echo it on a jQuery data
attribute for each widget's div
, like so:
<div class="widget" id="<?=$widget->id?> data-internal="<?= $widget->json_data?>
content
</div>
So every time I want to access the widget's content, I can read it from the element itself in jQuery, making it easy to access since I'm already doing something related to the div.
Another option I thought of doing, id to output the data in pure javascript, but then I would have to create an array of objects to access it, kinda like this.
<script>
$widget_array[<?= $widget->id?>] = <?= $widget->json_data ?>
</script>
So that I would have to get the widget ID and then look for the data inside the widgets javascript array.
While the first approach seems easier and more isolated and organized (each component holds the data for itself), the second seems a bit less "hackish".
My question is if there are any drawbacks or advantages on these two approches? Is it a bad practice to load a lot of data into a jquery data attribute? Would I have performance issues?