Skip to content

Argon, Active Record for JavaScript

gusortiz edited this page Jul 8, 2011 · 1 revision

What is it and what's with the name?

Argon is an asynchronous JavaScript implementation of the Active Record pattern, sounds complex I know, but is in fact quite simple once you get the hang of it, basically it just means that you will need a callback to manage the response of every method, just like the JQuery ajax method.

The name comes from the Argon element which symbol in the periodic table is ar (chosen to match Active Record initials).

Asynchrony

We say that a method or function is asynchronous when it runs independent from the main program flow, this gives us performance improvements but forces us to think in a new way to solve a problem. We are used to program in a more structured way in which we call one method after the other and feeding one method with the response of the other, for instance:

The Synchronous Way:

file = open(file_name);
content = file.read();
content << " add some text";

The Asynchronous Way:

file = open(file_name, callback);
callback(file) {
 return file.read() << " add some text";
}

What Argon offers

Argon provides a meta class with all the features required by the Active Record Model: getter methods, setter methods, object/DB relation, read methods, write methods and validations. Additionally we can save requests by caching the results.

Note: Requires Neon

Example:

Class("ExampleModel").includes(Argon.Model)({
  storage : (new Argon.Storage.JsonRest({
    url : {
      post   : '/path/to_post',
      get    : '/path/to_get',
      put    : '/path/to_put',
      remove : '/path/to_remove'
    }
  })),
  _cacheTimeToLive : {
    all      : 10000,
    instance : 2000
  }
  prototype : {
    name : null,
  }
});
ExampleModel.findByName('example name', function(data) {
   console.log(data.name); // example name
});

To ignore cache just feed the _cacheTimeToLive with 0 as time for both all and instance cache.