# Is there a best practice in dealing with javascript dependen

**URL:** <https://www.ruby-forum.com/t/is-there-a-best-practice-in-dealing-with-javascript-dependen/52087>\
**Category:** Rails\
**Created:** [January 10, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/is-there-a-best-practice-in-dealing-with-javascript-dependen/52087 "2006-01-10T20:40:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duane\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e19adc/32.png) [@Duane\_J](https://www.ruby-forum.com/u/Duane_J)\
**Post date:** [January 10, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/is-there-a-best-practice-in-dealing-with-javascript-dependen/52087/1 "2006-01-10T20:40:46Z")

</div>

As I’ve begun to work with several javascript libraries out there,  
I’ve run in to some dependency issues that must have been solved by  
others before.

For example, I’m trying to implement the jstree library from Laurie  
Harper in to BundledResource. Breaking the ‘jstree’ javascript from  
the sample html out in to its own file caused it to fail because the  
browser now tries to load both behaviour.js and (my new file)  
jstree.js at the same time–but jstree.js depends on the behaviour  
library to work.

The only solution I’ve found appears to be in a library I bumped in  
to from google, but I’m not sure it’s the best answer[1]. Has anyone  
else seen something easier and/or more “standard”?

Duane J.  
(canadaduane)  
[http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)

[1] [http://blog.metawrap.com/blog/](http://blog.metawrap.com/blog/)  
AutomaticDependencyResolutionForJavaScriptLibraries.aspx

---

<div class="post-metadata">

**Author:** ![Duane\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e19adc/32.png) [@Duane\_J](https://www.ruby-forum.com/u/Duane_J)\
**Post date:** [January 11, 2006, 11:28am UTC](https://www.ruby-forum.com/t/is-there-a-best-practice-in-dealing-with-javascript-dependen/52087/2 "2006-01-11T11:28:47Z")

</div>

Duane J. wrote:

> The only solution I’ve found appears to be in a library I bumped in to  
> from google, but I’m not sure it’s the best answer[1]. Has anyone else  
> seen something easier and/or more “standard”?  
> The only way I’ve found of cleanly dealing with dependencies is by doing  
> something like this:

function include(scriptname){  
document.writeln(  
‘’);  
}  
var global\_docs = {};  
function require(scriptname){  
if(!global\_docs[scriptname]){  
include(scriptname);  
global\_docs[scriptname]=true;  
}  
}

then you can call require(‘behaviour.js’) from jstree. This is probably  
horribly wrong in some respect, but it works for me so far…

---

<div class="post-metadata">

**Author:** ![Duane\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e19adc/32.png) [@Duane\_J](https://www.ruby-forum.com/u/Duane_J)\
**Post date:** [January 11, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/is-there-a-best-practice-in-dealing-with-javascript-dependen/52087/3 "2006-01-11T18:03:57Z")

</div>

On Jan 11, 2006, at 3:31 AM, Alex Y. wrote:

> > The only solution I’ve found appears to be in a library I bumped  
> > var global\_docs = {};  
> > –  
> > Alex

Actually, I recently discovered this technique being used by the  
scriptaculous library, so it can’t be that far off 😉 Can anyone  
else confirm that this is the best way to go? Does using AJAX to  
include libraries have any advantage over this method?

Thanks,  
Duane J.  
(canadaduane)  
[http://blog.inquirylabs.com/](http://blog.inquirylabs.com/)
