Using common Javascript code in several form_xx.js files

TonyBenedetti

Hobbyist Programmer & Fabrik Fan
I'd like to use a chunk of public domain code in several form_xx.js files. But if possible I'd like to have only 1 copy of the js to avoid maintaining it several times over.

It is a rather elaborate set of 60+ functions that converts dates expressed in one calendar system to another (Gregorian to Julian, or Mayan Tzolkin to ISO 8601, or, or, or -- more than 15 different systems).

Does anyone have any ideas where to place the file and then how to invoke the functions.

I tried to figure it out -- should be simple -- but my brain is fried. I figure some kind soul out there in the Fabrik Community (FabComm?) would have the answer off the top of their head and be able to say "Tony, you dimwit, all you have to do is...".

Thanks for listening to me whine and simper,
Tony
 
I do not know if this is the best way, but I use it.
Place your js file in components/com_fabrik/js.
In default.php file from your template folder add this code <script type="text/javascript" src="components/com_fabrik/js/your_filename.js"></script>
 
Super! Thanks! I'll give it a crack later today and post my results here. I'm sure that it works but I'm unsure nowadays about my ability to follow simple directions.

Thanks, Tony
 
I'm going to be introducing a way to do this built in fairly soon.

For now, a better way than directly adding it in a template file, is to add a PHP plugin to the form, run 'onLoad', and do ...

Code:
FabrikHelperHTML::script('components/com_fabrik/js/yourfile.js');

This has several benefits. You don't have to customize a template, and it uses the requirejs dependency loader, so it'll work correctly in popups (where scripts have to be loaded as dynamic assets).

You probably don't need to read beyond here, but for the insatiably curious ...

You can also get funky, and "do stuff", add a second arg with some inline JS, write your code as a class or a jQuery module, and give your module a name ...

Code:
$files = array(
   'MyUniqueName' => 'components/com_fabrik/js/yourfile.js')
};

$src = <<EOF
var myThing = new MyThing();
myThing.doesSomething();
EOF;

FabrikHelperHTML::script($files, $src);

If you look at the source of your page after doing either of those, you'll see something like this:

Simple case ...

Code:
requirejs(['http://localhost/joomla/components/com_fabrik/js/common.js'], function () {
});

Second case ...

Code:
requirejs(['http://localhost/joomla/components/com_fabrik/js/common.js'], function (MyUniqueName) {
var myThing = new MyThing();
myThing.doesSomething();
});

-- hugh
 
Thanks startpoint ! Thanks Hugh !!

I used Hugh's suggestion and it works a treat. Now I just need to write some Fabric elements to use those 60+ functions so that I can convert dates from the Julian calendar to the Hebrew calendar or to the French Revolutionary calendar or, or, or.

Who said programming isn't a fun hobby?

Thanks again,
Tony
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top