Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Java-Like Javascript Library Container //Javascript Object (See related posts)

   1  
   2  //+ Jonas Raoni Soares Silva
   3  //@ http://jsfromhell.com
   4  
   5  libraryControl = {
   6  	baseURI: '',
   7  	packs: {},
   8  
   9  	register: function( pack ){
  10  		for( var _, m, p = this.packs, pack = ( m = pack.split( '.' ) ).pop(); _ = m.shift(); p = p[_] || ( p[_]={} ) );
  11  		( p['%files%'] || ( p['%files%'] = {} ) )[pack] = {included:false,loaded:false};
  12  	},
  13  
  14  	findPackage: function( pack ){
  15  		for( var m, p = this.packs, pack = ( m = pack.split( '.' ) ).pop(); m.length && ( p = p[ m.shift() ] ); );
  16  		return !m.length && (p=p['%files%']) ? p[pack] : null;
  17  	},
  18  
  19  	isIncluded: function( pack ){
  20  		with( {x:this.findPackage( pack )} ) return x && x.included;
  21  	},
  22  
  23  	isLoaded: function( pack ){
  24  		with( {x:this.findPackage( pack )} ) return x && x.loaded;
  25  	},
  26  
  27  	include: function( pack ) {
  28  		var p = this.findPackage( pack ), pack = this.baseURI + pack.split( '.' ).join( '/' ) + '.js';
  29  		if( p ){
  30  			if( !document.body )
  31  				document.write( '<script type="text/javascript" src="'+pack+'"><\/script>' );
  32  			else with( {s: document.createElement( 'script' ) } ){
  33  				s.type = 'text/javascript';
  34  				s.src = pack;
  35  				document.body.appendChild( s );
  36  			}
  37  			return p.included = true;
  38  		}
  39  		return false;
  40  	},
  41  
  42  	require: function( pack ) {
  43  		var p = this.findPackage( pack );
  44    		if( p && !p.included )
  45  			return this.include( pack );
  46  		return false;
  47  	}
  48  }
  49  
  50  //libraryControl.register( 'library.test' );
  51  //libraryControl.require( 'library.test' );
  52  //libraryControl.include( 'library.test' );
  53  

Comments on this post

llucifer posts on Jul 02, 2005 at 12:20
Please remove the tag "java" as this does not relate to java ;-)

You need to create an account or log in to post comments to this site.


Click here to browse all 5349 code snippets

Related Posts