addFooterItem not adding js files

Permalink
I recently tried switching to using addFooterItem to add JS files to the footer of the page for an addon I have in the Marketplace. It fails to add my JS when adding a new copy of my addon to a page.

When adding a new block to an area, the JS I'm adding for my block using addFooterItem does not get added to the footer causing a JS error on the page. Refreshing the whole page seems to resolve the problem (JS is now properly included), but manually refreshing should not be necessary. If I change back to using addHeaderItem then everything works as expected as the JS gets included properly.

Any thoughts?

I have my code in the 'on_page_view' function and the line looks like this:

$this->addFooterItem($html->javascript('jquery.fancybox-1.3.4.pack.js', 'addon_name_here'));


Any help would be appreciated!

Thanks!

dwhillier
 
mnakalay replied on at Permalink Reply
mnakalay
That's weird, I use addFooterItem all the time and have no problem. Really addHeaderItem and addFooterItem are interchangeable.
What version of C5 are you using? addFooterItem is not available in older versions of C5
dwhillier replied on at Permalink Reply
dwhillier
Ya, that's what I thought, and I released a version a little while ago that used it. I wouldn't have released it if it didn't work ... don't know what happened. It's acting like the JS isn't even added to the footer.
dwhillier replied on at Permalink Reply
dwhillier
... using the latest C5 version (5.6.1.1) in a fresh, blank install with only my addon and the core addons.

Thanks for taking time to respond!
JohntheFish replied on at Permalink Reply
JohntheFish
This could be a sequencing issue. If the code that calls fancybox is not in a ready handler, then it could be executing as it loads, before fancybox is available.

So I would check that the code that calls fancybox is in a ready handler.

Another possibility is that there could be other blocks also loading fancybox. Some may have protection against collisions by specifying the additional optional parameter to addHeader/FooterItem or by a JavaScript test. If some do, and some don't, then loading order becomes relevant to collision prevention.
dwhillier replied on at Permalink Reply
dwhillier
I'm testing in a fresh version of the C5.6.1.1 using only my addon. The fancybox addon is just one of a few JS includes. If I comment it out, the error changes to something related to the next JS include not being found.

JS for my addon and all included plugins like fancybox are triggered by a single class init function and that call is in a block like this:
$(function(){
      ... pre-init variable declarations and setting default values  ...
      classInstance.init();
   });


If I refresh the page after a failed attempt to place the addon in an area on a page, everything is fine, the addFooterItem works and so does the addon. It's just immediately following adding the addon to a page area that fails. And again, the addHeaderItem always works.

I'll probably have to create a new 'hello world' addon and try adding things till it breaks again. Yum. 8|

Thanks for the suggestions!
RadiantWeb replied on at Permalink Reply
RadiantWeb
try putting it in the on_page_load() instead of view().

ChadStrat
dwhillier replied on at Permalink Reply
dwhillier
I tried using this in the controller, but nothing gets added to either the header or footer ... :(

Thanks for the suggestion!
RadiantWeb replied on at Permalink Reply
RadiantWeb
you tried using addFooter in both the view() method and the on_page_load() method in your block controller?

ChadStrat
dwhillier replied on at Permalink Reply
dwhillier
Yes, I tried all of the following in my addon's block controller:

function view()
   {
      // load helpers
      $html = Loader::helper('html');
      // load fancybox
      $this->addFooterItem($html->javascript('jquery.fancybox-1.3.4.pack.js', 'addon_name'));
      // load tiny scrollbar in 'mode_name'
      if($this->mode == 'mode_name'){
         $this->addFooterItem($html->javascript('jquery.tinyscrollbar.min.js', 'addon_name'));
      }
      // load addon js controller classes
      $this->addFooterItem($html->javascript('addon_name.js', 'addon_name'));
   }

function on_page_view()
   {
      // load helpers
      $html = Loader::helper('html');
      // load fancybox
      $this->addFooterItem($html->javascript('jquery.fancybox-1.3.4.pack.js', 'addon_name'));
      // load tiny scrollbar in 'mode_name'
      if($this->mode == 'mode_name'){
         $this->addFooterItem($html->javascript('jquery.tinyscrollbar.min.js', 'addon_name'));
      }
      // load addon js controller classes
      $this->addFooterItem($html->javascript('addon_name.js', 'addon_name'));
   }

function on_page_load()
   {
      // load helpers
      $html = Loader::helper('html');
      // load fancybox
      $this->addFooterItem($html->javascript('jquery.fancybox-1.3.4.pack.js', 'addon_name'));
      // load tiny scrollbar in 'mode_name'
      if($this->mode == 'mode_name'){
         $this->addFooterItem($html->javascript('jquery.tinyscrollbar.min.js', 'addon_name'));
      }
      // load addon js controller classes
      $this->addFooterItem($html->javascript('addon_name.js', 'addon_name'));
   }
dwhillier replied on at Permalink Reply
dwhillier
oh, and the view has additional code to actually output the markup for the addon :)
RadiantWeb replied on at Permalink Reply
RadiantWeb
I normally just add any js needed into the block view's js folder. C5 auto loads those.

ChadStrat
mnakalay replied on at Permalink Reply
mnakalay
I know it's going to sound stupid but I had problems before because my JavaScript file name contained the word jQuery. You might want to try that.
mnakalay replied on at Permalink Reply
mnakalay
And also I think for this code to work the files have to be in the js folder of the package not the block. If they're in the js folder of the block they will be loaded automatically as Chad said and there might be a conflict.
dwhillier replied on at Permalink Reply
dwhillier
My JS is in my packages JS folder. Several are included conditionally depending on config settings when adding the block to a page so I unfortunately can't just include them in block's js folder for auto inclusion.

It's not like I can't get it to work at all, if I switch everything to addHeaderItem instead of addFooterItem then all is good. Was just wanting my JS to be included in the footer so my addon could be a 'good neighbor' to page load stats. ;)

Thanks all for the suggestions! If I figure anything out, I'll post back.