I’m writing this post mainly to think through a javascript library I’m working on.

The library is called ActiveForm, and the concept is similar to how ActiveRecord objects in Rails interact with the database. ActiveForm, or rather its children ActiveFormField objects get and set values from actual input elements, while utilizing railsish callbacks. Including:

before_validate()
validate()
before_set()
set()
after_set()

before_get()
get()

This allows me to do something like this, where I format a the value as an amount before setting actual element’s value:

before_set: function(value) {
return this.format_as_money(value);
}

Whether or not this is the best way to do things, I couldn’t tell you. I’m no javascript genius. But I’m having fun writing it and I’m enjoying they syntax it creates.

A question that I’m asking myself now, that I know will come up again, is why didn’t I just extend the form elements themselves? This would be similar to how Prototype does with “$(input).getValue();“. So I’m documenting it here so when I forget why later I can look it up.

For starters, it had to do with how I was thinking of the objects versus the actual elements. In my head, the elements were like the database in Rails and the objets were like ActiveRecord objects. ActiveRecord objects cache and process database values and only call and update the database when necessary. Forgive me if I’m explaining Rails all wrong, I’m still a relative newbie.

This metaphor loses a little when you know that the user edits the elements directly, but ignore that part. :) It doesn’t actually make that big of a difference since the ActiveFormField immediately grabs the value and processes it.

Another reason would be containment of the ActiveForm methods, so they don’t conflict with anything else. I don’t know if this is a huge problem though.

At the moment, I can’t think of any other reason, so I may have to try switching and see what I run into. I think it’ll be a lot of work though.

And now somehow hours have gone by in a blink, and I have to go to bed, where I’ll stare at the ceiling and think about this all night…