I am passing some params to JS in my custom module using the addScriptOptions provided by Joomla:
$document->addScriptOptions("mod_mymodule", $params));
This works fine, as long as there is only 1 instance of the same module on 1 page. But how to make it work for multiple modules on the same page?
I would think to pass 1 object like so:
$document->addScriptOptions("mod_mymodule", (object)array($params => $module->id)));
In my JavaScript file I can log the object:
console.log(Joomla.getOptions('mod_mymodule'));
// Output:
{
105: {...}
}
So the problem is that only the first module is passed in the object. I need every module to be passed in the object before it is passed to my JS file, so I can loop over the object to create desired script for each module.
Addition: I think I'm getting closer, but still not working:
$currentOptions = $document->getScriptOptions('mod_mymodule');
if ($currentOptions) :
$currentOptions->{$module->id}->{$params};
else :
$document->addScriptOptions("mod_mymodule", (object)array($module->id => $params));
endif ;
I think the logic is there, or not? Syntax error? I'm not sure..
Any insights? Thanks!