Forum: Ruby on Rails Is there a best practice in dealing with javascript dependen

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Duane J. (Guest)
on 2006-01-10 21:40
(Received via mailing list)
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/


[1] http://blog.metawrap.com/blog/
AutomaticDependencyResolutionForJavaScriptLibraries.aspx
Alex Y. (Guest)
on 2006-01-11 12:28
(Received via mailing list)
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(
     '<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...
Duane J. (Guest)
on 2006-01-11 19:03
(Received via mailing list)
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/
This topic is locked and can not be replied to.