Where to Put Block JS That's Shared Between View and Edit?
Permalink 1 user found helpful-Steve

So am I correct in assuming you're suggesting I use the "helper" methods to include the JS in the doc head?
I'll give it a try...
-Steve
Perhaps I should simply resort to JS to do this...
-Steve
$html = Loader::helper('html'); $bv = new BlockView(); $bv->setBlockObject($this->getBlockObject()); $this->addHeaderItem($html->javascript($bv->getBlockURL() . '/auto.js'));
now it will be outputted in the view as well.
-Jordan
Your suggestion will be a good solution under most circumstances. What I neglected to mention, however, is that in my particular case, the block is part of a package, and I wish to make the JS available to other developers. (It works in conjunction with a PHP script that resides in the "libraries" directory in the package root.) I decided, therefore, to put the JS inside a "js" directory in the package root so that a developer can load it like so...
$hh = Loader::helper('html'); $this->addHeaderItem( $hh->javascript('pkgscript.min.js', $pkgNam) );
This seems to be the way that shared JS is intended to be referenced when it's part of a package. So instead of requiring developers to load the JS in a "non-standard" way, I decided that I would jump through the hoop instead. So when the block is in edit mode, I add a reference to the script via jQuery like so...
var script_url = <?php echo $this->getBlockURL(); ?> + '/../../js/pkgscript.min.js'; if (!($('head').children('script[href*=' + script_url + ']').length)) { $('head').append($('<script>').attr({ 'type' : 'text/javascript', 'src' : script_url })); }
If you know of a better way to go about this, I'd love to hear it.
-Steve
-Jordan