You have a simple config table - key:string, value:string. Say you want to read the value for the key "foo". You end up with code that looks something like this:
var stmt = new air.SQLStatement();
stmt.sqlConnection = conn;
stmt.text = "select value from config where key = :key";
stmt.parameters[":key"] = "foo";
stmt.addEventListener(air.SQLEvent.RESULT, function(){
var fooValue = stmt.getResults().data[0];
doStuffWithFoo...
});
stmt.execute();
Which makes me throw up in my mouth a little bit. I want to do something like this:
var fooValue = getConfig("foo");
doStuffWithFoo...
But that damned async call just keeps getting in the way.
Adobe hasn't committed to building this yet "because of the development effort it would require". My thoughts on that can be summed up in my previous post: "Programmers are expensive, CPUs are cheap" does not justify rapid development of slow code.
So, I wrote some convoluted code, scream "fuck this shit!" a couple of time, and wrote a little utility to provide a synchronous API - the Big Dumb Javascript AIR Configuration. To install, include Configuration.js:
<script src="Configuration.js"></script>
You then have to initialized the configuration system.
BigDumbDev.Configuration.init( function(){ air.trace('configuration ready'); } );
Reading and writing configs goes like this:
BigDumbDev.Configuration.getConfig('key');
BigDumbDev.Configuration.setConfig('key', 'value');
BigDumbDev.Configuration.removeConfig('key');
Sets and removes are immediately available, so you can do this:
BigDumbDev.Configuration.setConfig('foo','bar');
var foo = BigDumbDev.Configuration.getConfig('foo');
// foo is bar
You can see all the details in the source code. There is no magic - it still does the DB calls asynchronously. I cache the config values and read from the cache. Update change the cache and then kick of the async call to update the database.
You can download a sample AIR app that utilizes the system.
1 comments: