Duane Johnson
2006-Jan-10 19:38 UTC
[Rails] Is there a best practice in dealing with javascript dependencies?
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 Johnson (canadaduane) http://blog.inquirylabs.com/ [1] http://blog.metawrap.com/blog/ AutomaticDependencyResolutionForJavaScriptLibraries.aspx -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060110/5694bc72/attachment.html
Alex Young
2006-Jan-11 10:28 UTC
[Rails] Is there a best practice in dealing with javascript dependencies?
Duane Johnson wrote:> 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"?The only way I''ve found of cleanly dealing with dependencies is by doing something like this: function include(scriptname){ document.writeln( ''<script type="text/javascript" language="JavaScript1.2" src="'' + scriptname + ''"></script>''); } 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... -- Alex
Duane Johnson
2006-Jan-11 17:01 UTC
[Rails] Is there a best practice in dealing with javascript dependencies?
On Jan 11, 2006, at 3:31 AM, Alex Young wrote:> Duane Johnson wrote: >> 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"? > The only way I''ve found of cleanly dealing with dependencies is by > doing something like this: > > function include(scriptname){ > document.writeln( > ''<script type="text/javascript" language="JavaScript1.2" src="'' + > scriptname + ''"></script>''); > } > 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... > > -- > AlexActually, 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 Johnson (canadaduane) http://blog.inquirylabs.com/