Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

function headerDeclarations loaded too many times 12 Aug 2013 09:51 #10749

Sometimes there is the need to load the component header in several places, for example using a plugin or a module (or many modules), it's done callng the function:

headerDeclarations

you'll find it in the helper.php file.
Everything works, BUT if you take a look on the source code of the generated page, you'll find out that all the script declarations (for example those for the jquery validationengine) are loaded each time the headerDeclaration is called, so for example if you have 1 plugin and 4 modules, it's loaded 5 times (+1 if you are in a view of your component).

my workaround is to add a static variable $loaded to the function in this way:
	public static function headerDeclarations()
	{
		static $loaded = false;
		
		if(!$loaded){
			$app = JFactory::getApplication();
			$doc = JFactory::getDocument();

			$siteUrl = JURI::root(true);

			$baseSite = 'components' .DS. COM_ESTIMATOR;
			$baseAdmin = 'administrator' .DS. 'components' .DS. COM_ESTIMATOR;

			$componentUrl = $siteUrl . '/' . str_replace(DS, '/', $baseSite);
			$componentUrlAdmin = $siteUrl . '/' . str_replace(DS, '/', $baseAdmin);

			//Javascript
			//jQuery Loading : Abstraction to handle cross versions of Joomla
			JDom::_('framework.jquery');
			JDom::_('framework.jquery.chosen');
			JDom::_('framework.bootstrap');

			$doc->addScript($siteUrl . '/media/system/js/core.js');

			//Load the jQuery-Validation-Engine (MIT License, Copyright(c) 2011 Cedric Dugas http://www.position-absolute.com)
			self::addScript($doc, $baseAdmin, 'js' .DS. 'jquery.validationEngine.js');
			self::addStyleSheet($doc, $baseAdmin, 'css' .DS. 'validationEngine.jquery.css');
			EstimatorHelperHtmlValidator::loadLanguageScript();

			//CSS
			if ($app->isAdmin())
			{
				// Blue stork override
				$styles = "fieldset td.key label{display: block;}fieldset input, fieldset textarea, fieldset select, fieldset img, fieldset button{float: none;}fieldset label, fieldset span.faux-label{float: none;display: inline;min-width: inherit;}";
				$doc->addStyleDeclaration($styles);

				self::addStyleSheet($doc, $baseAdmin, 'css' .DS. 'estimator.css');
				self::addStyleSheet($doc, $baseAdmin, 'css' .DS. 'toolbar.css');
				$doc->addStyleSheet($componentUrlAdmin . '/css/jquery-ui-1.9.1.min.css');
			}
			else if ($app->isSite())
			{
				self::addStyleSheet($doc, $baseSite, 'css' .DS. 'estimator.css');
				self::addStyleSheet($doc, $baseSite, 'css' .DS. 'toolbar.css');
				$doc->addStyleSheet($componentUrl . '/css/jquery-ui-1.9.1.min.css');
			}
			
			$loaded = true;
		}
	}

this will solve the problem, but if you have custom js/css desclarations for a plugin or module, it's better if you move them out of the function headerDeclarations()
The following user(s) said Thank You: admin

Please Log in or Create an account to join the conversation.

function headerDeclarations loaded too many times 12 Aug 2013 12:34 #10755

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
Yes, good suggestion.

But this is strange because Joomla should handle this natively.

I think the only problem could come from :
XxxxHelperHtmlValidator::loadLanguageScript();

But, as you suggest, it is even better to avoid the loading when we know that anyway it has already been loaded.
I keep your suggestion.
K++
Coding is now a piece of cake

Please Log in or Create an account to join the conversation.

function headerDeclarations loaded too many times 12 Aug 2013 12:40 #10756

I don't know why joomla doesn't manage that and as you know the joomla code is "untouchable" so I opted for another solution.

Please Log in or Create an account to join the conversation.

function headerDeclarations loaded too many times 12 Aug 2013 13:41 #10761

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 986
Fixed.
Coding is now a piece of cake
The following user(s) said Thank You: Tomaselli

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Time to create page: 0.081 seconds

Real time saver and great Component Builder tool ! I have been developing with J-Cook Pro Component Builder for several months now and can say with all honesty that this product/service is second to none. The product is feature rich and is being improved and added to all the time. Do yourself a favor if you need to build a Joomla! Component then you can do no wrong in trying the product. You will save on time and effort while being able to deliver your project on time. J-Cook pro does the hard work for you you then have the freedom to fully customise the end result for your own needs. 

One word: Awesome.
Edwardcox (JED)
         

Get Started