ZF-8794: addJavascriptFile() with support to prefix with javascript code
Description
There is problem to load jQuery plugins which require some configuration in jQuery namespace before loading of js file. As an example I will use jqGrid library.
Following code will not work, because jQuery() will render always files before embeded scripts and we need to set useJSON before jquery.jqGrid.min.js, but after grid.locale-en.js.
echo $this->jQuery()
->addJavascriptFile($jqgridLibPath . '/js/i18n/grid.locale-en.js')
->addJavascript('jQuery.jgrid.useJSON = true;')
->addJavascriptFile($jqgridLibPath . '/js/jquery.jqGrid.min.js');
With Zend_View_Helper_HeadScript I could do following:
echo $this->headScript()
->appendFile($jqgridLibPath . '/js/i18n/grid.locale-en.js')
->appendScript('jQuery.jgrid.useJSON = true;')
->appendFile($jqgridLibPath . '/js/jquery.jqGrid.min.js');
Solution could be to change the rendering order to comply with Zend_View_Helper_HeadScript, so it will mix embeded and loaded Javascript in order it was added.
Other solution could be to add function like addJavascriptBetweenFiles() and only that would keep the order:
echo $this->jQuery()
->addJavascriptFile($jqgridLibPath . '/js/i18n/grid.locale-en.js')
->addJavascriptBetweenFiles('jQuery.jgrid.useJSON = true;')
->addJavascriptFile($jqgridLibPath . '/js/jquery.jqGrid.min.js');
Comments
Posted by Benjamin Eberlei (beberlei) on 2010-01-29T01:49:29.000+0000
Not a blocker, I have to think how to solve this.