<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>occasionally useful &#187; library</title>
	<atom:link href="http://blog.maxaller.name/tag/library/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.maxaller.name</link>
	<description>ruby, ubuntu, etc</description>
	<lastBuildDate>Sun, 11 Jul 2010 07:01:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>LockNLoad is&#8230;locked and loaded?</title>
		<link>http://blog.maxaller.name/2009/12/locknload-is-locked-and-loaded/</link>
		<comments>http://blog.maxaller.name/2009/12/locknload-is-locked-and-loaded/#comments</comments>
		<pubDate>Sun, 06 Dec 2009 22:37:30 +0000</pubDate>
		<dc:creator>Max</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[inversion of control]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[locknload]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[titanium]]></category>

		<guid isPermaLink="false">http://blog.maxaller.name/?p=236</guid>
		<description><![CDATA[I've just completed and pushed the initial version of LockNLoad, a Spring-like Inversion of Control container for Javascript, which follows the dependency inversion principle (I think I'm getting all these terms right...).  Given the proper configuration, you can simply say LNL.$("my_id") and get a prototype or singleton object or function.  Read on for [...]]]></description>
			<content:encoded><![CDATA[<p>I've just completed and pushed the initial version of LockNLoad, a <a href="http://www.springsource.org/" target="_blank">Spring</a>-like Inversion of Control container for Javascript, which follows the dependency inversion principle (I think I'm getting all these terms right...).  Given the proper configuration, you can simply say LNL.$("my_id") and get a prototype or singleton object or function.  Read on for more information.<br />
<span id="more-236"></span><br />
The background story is I wanted to make a game library in Javascript for an experiment with <a href="http://www.appcelerator.com/" target="_blank">Titanium</a>, and I found that one of my components was Titanium-specific (the one for playing sound).  My game library was generic enough to be used in any browser except for the sound feature, and so I found myself passing around a reference to the sound function I needed to instantiate based on the platform.  But that's dumb!  I should just be able to say container.get("sound_type") and instantiate that, and let the container figure out what platform I was on and what sound object I needed.  So I made LockNLoad, which is that container.</p>
<p>Carrying on with the previous story, my classes might look like this:</p>
<pre><code>
function RegularSound(){
  this.playSound = function(snd){
    $(document).append("<sound src=\"" + snd + "\" />");
  }
}

function TitaniumSound(){
  this.playSound = function(snd){
    Titanium.Media.Sound.play("app://" + snd);
  }
}
</code></pre>
<p>Normally, your options are to pass around a reference to the function so you can instantiate it, put "if titanium instantiate TitaniumSound else instantiate RegularSound" everywhere in your code, or create a function that abstracts away the method call in the previous suggestion.</p>
<p>Following the dependency inversion principle, you ARE abstracting away the function that creates the Sound object.</p>
<p>Continuing: configuration:</p>
<pre><code>
var config = {
    "sound": {
        "class": RegularSound
    }
};
if(Titanium){
  config.sound.class = TitaniumSound;
}
LNL.loadConfig(config);
</code>
</pre>
<p>Finally, the calling code would look like this:</p>
<pre><code>
var mySound = LNL.$("sound");
mySound.playSound("monkey_laugh.mp3");
</code></pre>
<p>It can do other stuff too, like singleton, setting properties on the returned object, and returning raw functions.  The example given is the simplest possible functionality it can provide.</p>
<p>Interested?  It's up on github, with a complete README with all the details.  Link: <a href="http://github.com/nanodeath/LockNLoad">http://github.com/nanodeath/LockNLoad</a></p>
<p>It doesn't do dependency injection yet, and might never if no one cares <img src='http://blog.maxaller.name/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  but it would be nice if it could store arbitrary objects, not just classes and functions, so I'll be adding that soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.maxaller.name/2009/12/locknload-is-locked-and-loaded/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
