Saturday, September 6, 2008

jQuery style result sets for OR-Mapper

I implemented a couple shortcuts that enable working with datasets in a jQuery style.

As an example, the sticky note code had a couple lines that looked like this:

Note.selectAll(function (notes) {
notes.each(function (note) {
note.redraw()
note.show()
})
})


The API is inherently asynchronous because that is the only way that is provided by the HTML5 database API.

This can now be written as:
Note.selectAll().draw().redraw().show()


This is pretty dark JavaScript magic going on, because all methods after selectAll won't really be executed until the select has actually been executed.
I like this API style much better, but it has some serious draw backs:
  • In the Gears version you loose the transaction context because evil global vars are involved (in my mock up implementation of the HTML5 api)
  • If there is something coming in the next line, one would expect it to be executed after show() which is not the case.

3 comments:

DanB said...

Have you considered using Arrows for this sort of thing? Although that is perhaps even darker magic that what you have done here ...

DanB said...

Have you considered using Arrows for this sort of thing? Although that is perhaps even darker magic that what you have done here ...

Malte said...

Arrows definitely seem to go into a similar direction.

My personal intent was to make the API look as synchrononous as possible for some common cases to make the code look more expressive and readable. I think that Arrows would add an extra layer of complexity that might not work in that direction.