I'm hoping I can get some solid advice here.. I've been searching the Joomla documentation on this topic, but couldn't find it, or I was looking in the wrong places.
I'm building a Joomla component, which does quite a lot of heavy lifting (retrieving JSON from an external API). Therefore, I'd like to load most of the component's views using AJAX, asynchronous from the main site. The component also has some views which I'd like to load in certain module positions (e.g. in a sidebar on the right of the page).
To achieve this, I've created a Custom HTML module that performs an AJAX call, which, most importantly, adds 'format=raw' to the GET parameters of the request. This triggers Joomla to only return the component output HTML, and not the whole page (which is what you would receive without format=raw).
And here is my problem: the component also tries to load a bit of its own CSS and JavaScript. This is done the regular way, via:
$document = JFactory::getDocument();
$document->addScript(..scriptfiles..);
$document->addStylesheet(..stylesheets..);
But because of the 'raw' component output, nothing is done with these additional script declarations! They obviously do not end up in the <head> of the document, because the page has already been rendered.
Is there a way to make Joomla still include the CSS and JavaScript files while also rendering only the raw component HTML? Some expert advice would be most welcome!
I have looked at the following options:
1. Include the scripts and CSS into the template that I'm using, to make sure they are already loaded when I load the rest of the component via AJAX. This can be OK, but it ties the component to the template which is being used.. It sounds to me like my only real option, but I'm still hoping for a more elegant way to handle this :).
2. Instead of 'format=raw', use 'tmpl=bare' and create (again, inside the template, not the component..) a new 'bare' template that loads only the head and the component, like so:
<jdoc:include type="head" />
<jdoc:include type="component" />
This also works, but has an annoying disadvantage that I get all kind of other tags in the middle of my HTML (<base>, <meta>, <title>, <link>), which is not valid HTML anymore..
Any help is greatly appreciated, thanks in advance for your time! I can provide more detailed code examples and a link to the site if someone cares to have a look.
Kind regards,
Pieter
wait : true,dataType : 'text',error : function(model, fail, xhr) { alert(fail.responseText);}
var body = resp.split(/@My-START@|@My-END@/);if (body.length === 3) { return JSON.parse(body[1]);}