ActiveRecord JavaScript Library Announced AIR Support

Ryan Johnson of Aptana announced the beta version of ActiveRecord.js, an open source JavaScript library that implements the ActiveRecord pattern popularized by the Ruby on Rails community. In addition to supporting Adobe AIR, it provides support for other JavaScript application runtimes including web browsers. On the server-side, ActiveRecord.js also works with MySQL and SQLite via Aptana Jaxer, the Ajax server based on the Mozilla browser engine. Work is underway as well to support HTML 5’s SQL APIs, now available in some versions of WebKit, once that specification is finalized. 

For developers creating Ajax applications using Adobe AIR, this means they can persist JavaScript objects and data using pure JavaScript syntax. All the underlying SQL commands are simplified into higher level APIs that are more natural feeling for JavaScript developers. In many cases, the syntax results in far fewer lines of code to implement as well. 

There is an Adobe AIR sample application provided with the project that demonstrates a basic example of the ease of use. 

active-record.png

As shown below, it takes one line to connect ActiveRecord.js and a few lines more to define a record object, in this case Note.

      ActiveRecord.connect(ActiveRecord.Adapters.AIR);
      
      Note = ActiveRecord.define(‘notes’,{
        body: ”,
        created: {
          type: ‘DATETIME’
        }
      });
            
With the Note type record defined, getting a collection of records is a easy as Note.find() as shown below.

      window.onload = function(){
        //display all existing persisted notes
        var notes = Note.find({
            all: true
        });
        for(var i = 0; i < notes.length; ++i){
            display_note(notes[i]);
        }

Note.create() not surprisingly adds a new Note record.

        //form logic to create new notes
        document.getElementById(‘add_note_button’).onclick = function(){
          var note = Note.create({
            body: document.getElementById(‘add_note_text’).value
          });
          display_note(note);
        };
      };

Perhaps the most verbose part of the sample is where it directly updates the user interface using native HTML DOM APIs. Of course, this could be further simplified by using jQuery, Dojo Toolkit, Ext JS and other popular Ajax libraries. ActiveRecord.js is intended to complement, not replace, the use of other Ajax libraries. 

Aptana has also put a .project file into the source repository for this example to make it easy to import into Aptana Studio and run this with the Adobe AIR Development Plugin. Since the project contains JavaScript files, you can also use Dreamweaver or any other text editor to view it as well.

Related Links:

2 Responses to ActiveRecord JavaScript Library Announced AIR Support

  1. David Rivers says:

    Also check out JazzRecord: http://www.jazzrecord.org/ which also support Air as well as Google Gears and Appcelerator Titanium!

  2. Awesome, topic!! I just had to comment.