/JSDIAAL

Primary LanguageJavaScript

JSDIAAL (JavaScript Dependency Injection And AutoLoading)

  • About
  • License
  • Installation
  • Examples
  • Author

About

What the hell is JSDIAAL?

JSDIAAL was made in the need of having dependency configuration and injection in JavaScript applications. Furthermore, if you typically create a web application, you are required to load all available resource files. Even if the client never uses them. What a waste of resources!

JSDIAAL' s part in your web application: check if a requested resource exists, load it if not, instantiate or return an already instantiated class.

Sound difficult? Nope.

joii?

There are a couple of great JavaScript frameworks out there. And jQuery. I really hate jQuery. Why? Ever tried to declare classes, interfaces, abstracts, inheritance with jQuery without another framework? Jepp, that's why joii!

If you are in the lucky position to have MooTools or another great JavaScript framework, ignore joii yet. But take a look later!!

License

MIT, general free, use it, extend it, love it

Installation

First of all, include the needed files.


    < script src="js/vendor/joii/joii.js"></script>
< script src="js/vendor/joii/plugins/joii.mixin.js"></script><br />
< script src="js/configuration/dependency.js"></script><br />
< script src="js/MyLib/Dependency.js"></script><br />
< script src="js/Application.js"></script><br />

The rest does JSDIAAL for you!

almost...

Make sure, all declared classes have dedicated files.
Your declared a class MyLib_Foo_Bar? Put it under /js/MyLib/Foo/Bar.js!
If you have it somewhere other than js/, change MyLib/Dependency.js...

js/configuration/dependency.js

Here is what is the most essential part in JSDIAAL: the configuration of your dependencies.

var dependencyConfiguration = {
	foobar: { // name of your dependency
		shared: true, // share this if instantiated!
		className: 'MyLib_Foobar' // which class is meant?
	},
	pewpew: {
		shared: false, // always create a fresh instance!
		className: 'MyLib_PewPew' // 
	},
	user: {
		shared: false,
		className: 'MyLib_Model_User'
	}
};

js/MyLib/Dependency.js

The loading, instantiating an managing class.

js/Application.js

What is really needed: global accessible variable:

var dependency = null;

If document is fully loaded, instantiate the dependency class:


window.addEventListener('load', function(){
dependency = new MyLib_Dependency();
});

Now JSDIALL is ready to use:


dependency.getPewPew().cathew()
dependency.getPewPew().getCounter()
dependency.getFoobar().doFoo()
dependency.getFoobar().getCounter()

See index.html for a full example! Have an eye on FireBug or whatever shows you the loaded files and JavaScript console ;)

Author