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

About this user

Jonas Raoni Soares Silva http://jsfromhell.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Java-Like Javascript Library Container //Javascript Object

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com

libraryControl = {
	baseURI: '',
	packs: {},

	register: function( pack ){
		for( var _, m, p = this.packs, pack = ( m = pack.split( '.' ) ).pop(); _ = m.shift(); p = p[_] || ( p[_]={} ) );
		( p['%files%'] || ( p['%files%'] = {} ) )[pack] = {included:false,loaded:false};
	},

	findPackage: function( pack ){
		for( var m, p = this.packs, pack = ( m = pack.split( '.' ) ).pop(); m.length && ( p = p[ m.shift() ] ); );
		return !m.length && (p=p['%files%']) ? p[pack] : null;
	},

	isIncluded: function( pack ){
		with( {x:this.findPackage( pack )} ) return x && x.included;
	},

	isLoaded: function( pack ){
		with( {x:this.findPackage( pack )} ) return x && x.loaded;
	},

	include: function( pack ) {
		var p = this.findPackage( pack ), pack = this.baseURI + pack.split( '.' ).join( '/' ) + '.js';
		if( p ){
			if( !document.body )
				document.write( '<script type="text/javascript" src="'+pack+'"><\/script>' );
			else with( {s: document.createElement( 'script' ) } ){
				s.type = 'text/javascript';
				s.src = pack;
				document.body.appendChild( s );
			}
			return p.included = true;
		}
		return false;
	},

	require: function( pack ) {
		var p = this.findPackage( pack );
  		if( p && !p.included )
			return this.include( pack );
		return false;
	}
}

//libraryControl.register( 'library.test' );
//libraryControl.require( 'library.test' );
//libraryControl.include( 'library.test' );

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS