Unfortunatelly I can't register on the joomla forum, so I'll try my best here.
Please help, is there any way to control the order of loaded scripts.
Example: in my template's index.php I do:
as the very first addScript(); So I load that script globally for all pages.
Then, in the default.php of my custom component I do:
to add additional behaviour for that very component view specifically.
Logically I would assume that the addScripts in the index.php are added to the $doc object first and after that the addScripts in the component view are appended $doc object.
But what I end up with in the head is:
<script src="my/script_b.js"></script> <script src="my/script_a.js"></script>
This bugs me pretty hard since my/script_a.js performes calculations my/script_b.js depends on, so the order is crucial.
Please, there must be a way to control the order of loading scripts otherwise this feature is pretty much pointless, isn't it?
First, to understand why what you're doing is causing the problem you need to know the Joomla lifecycle.
So when you call addScript in your template it gets added to the Document after the script added by your component.
Second, to the solutions...
If the script loaded by your template has no dependencies then just write it inline above your
<jdoc:include type="head" />
Custom head tags are loaded after the scripts so in your component don't use addScript, use addCustomTag instead. eg.
Write a system plugin and in the onAfterRoute method first load your global script and then see if the option in the JInput is your component and then load your component script if it is (or just load it in your component tmpl as you're already doing).
In the template get the head data from the JDocument and rustle it how ever you like.