views:

69

answers:

1

I'm using the relatively immature Joose Javascript ORM plugin (project page) to persist objects in an Appcelerator Titanium (company page) mobile project. Since it's client side storage, the application has to check to see if the database is initialized before starting up the ORM since it inspects the DB tables to construct the classes.

My problem is that this sequence of operations (and if this one is like this, other things down the road) takes a lot of callbacks to complete. I have a lot of jumping around in the code that isn't apparent to a maintainer and results in some complex call graphs and whatnot. So, I ask these questions:

  1. How would you asynchronously initialize a database and populate it with seed data using an ORM that needs the schema to be correct to function?
  2. Do you have any general strategies or links for async/event driven programming and keeping the call graph simple and understandable?
  3. Do you have any suggestions for Javascript ORMs/meta object systems that work with HTML 5 as a storage engine and are hopefully framework agnostic?
  4. Am I just a big newb and should be able to work this out with ease?

Thanks folks!

+1  A: 

Take a look at NarrativeJS:

Narrative JavaScript is a small extension to the JavaScript language that enables blocking capabilities for asynchronous event callbacks. This makes asynchronous code refreshingly readable and comprehensible.

With Narrative JavaScript fetching a document using XmlHttp goes from looking like this:

function handleResponse(responseText) {
    document.getElementById("myElem").innerHTML = responseText;
}
fetch("http://www.url.com/", handleResponse);

to this:

document.getElementById("myElem").innerHTML = fetch->("http://www.url.com/");

Too bad the project is no longer active :-(

dojo.Deferred() also seems to implement an async monad, though the syntax is not as clear as NarrativeJS.

There has been some work on bringing async sugar to CoffeeScript, but ultimately it wasn't accepted.

RxJS is another one that deals with this.

Mauricio Scheffer