1 (edited by Neck 2008-12-13 18:51)

Topic: [Advice] Multiple extensions using prototype

Sharing some thought to other extension developers...

<hook id="hd_head"><![CDATA[
    $forum_head['ex1_prototype'] = '<script type="text/javascript" src="'.$ext_info['url'].'/media/js/prototype.js"></script>';
]]></hook>
-------
<hook id="hd_head"><![CDATA[
    $forum_head['ex2_prototype'] = '<script type="text/javascript" src="'.$ext_info['url'].'/media/js/prototype.js"></script>';
]]></hook>

I've come across a problem while developing an extension: what if both of them require prototype (javascript framework). Adding it twice is not only a waste, but would most likely provokes bunch of bugs, I could separate it as a third extension that others would require to run, but this is bothersome for users.
Looking at how js files are added to the header I finally found a satisfying work around.


<hook id="hd_head"><![CDATA[
    $forum_head['prototypejs'] = '<script type="text/javascript" src="'.$ext_info['url'].'/media/js/prototype.js"></script>';
]]></hook>
-------
<hook id="hd_head"><![CDATA[
    $forum_head['prototypejs'] = '<script type="text/javascript" src="'.$ext_info['url'].'/media/js/prototype.js"></script>';
]]></hook>

First we need to choose a single name for prototype across all extensions, I took "prototypejs".
This way, no matter how many extensions add it, only one file will be loaded because the last extension will override previous value for the "prototypejs" key.


<hook id="hd_head"><![CDATA[
    $forum_head['prototypejs'] = '<script type="text/javascript" src="'.$ext_info['url'].'/media/js/prototype.js"></script>';
]]></hook>
-------
<hook id="hd_head"><![CDATA[
if(FORUM_PAGE === 'index') {
    $forum_head['prototypejs'] = '<script type="text/javascript" src="'.$ext_info['url'].'/media/js/prototype.js"></script>';
}
]]></hook>

One problem remains, let's imagine 2 extensions, one run only on index and the other applies on each page. The index one is parsed last. We would have 2 different files loaded depending on the page. This is a waste of time, bandwidth and cache.


<hook id="hd_head"><![CDATA[
    $forum_head['prototypejs'] = '<script type="text/javascript" src="'.$ext_info['url'].'/media/js/prototype.js"></script>';
]]></hook>
-------
<hook id="hd_head"><![CDATA[
if(FORUM_PAGE === 'index') {
    if(!isset($forum_head['prototypejs'])) { $forum_head['prototypejs'] = '<script type="text/javascript" src="'.$ext_info['url'].'/media/js/prototype.js"></script>';}
}
]]></hook>
-------
<hook id="hd_head" priority="4"><![CDATA[
if(FORUM_PAGE === 'index') {
    $forum_head['prototypejs'] = '<script type="text/javascript" src="'.$ext_info['url'].'/media/js/prototype.js"></script>';
}
]]></hook>

There's two way to solve this, either up the priority of the index's extension so it runs first, either add a condition to not override already existing prototypejs key.


That's all, hope it may be helpful to someone. It can applies to lot of things you wish to include only once across many extensions wink

Re: [Advice] Multiple extensions using prototype

Coming up with a list of commonly-used libraries and assigning each one of them a common name would help even more.

Here's a list of JS frameworks/libraries: http://ntt.cc/2008/02/13/the-most-compl … -list.html

3 (edited by hcs 2008-12-14 15:11)

Re: [Advice] Multiple extensions using prototype

This good idea.
I have made extension jsHelper. The same idea - to prevent collisions.
Except for it  jsHelper solved other task - to include a js-script in html-header at any time generating of a content. Use hook hd-head and in general anyone hooks from header.php narrows opportunities of the developer. It happens it is necessary to include a js-script already after header.php has been caused.
jsHelper extension solves also this problem too.
That it worked, it is necessary to have js-scripts in one directory on server, for example root/js/
It should become the agreement.

Example of connection jquery:

$jsHelper->add ($base_url.'/js/jquery.js');

Some extensions, can  make this call.
In html-header will be only once:

<script type = "text/javascript" src = "http://example.forum/js/jquery.js"> </script>

Try extension: http://punbb.ru/downloads/jshelper.zip (1kb)

sorry my english

Re: [Advice] Multiple extensions using prototype

Does this solution still work if the extension developer wants to put the js library at the end of the page so it doesn't stall the loading of the page?