Light-weight JSON Binding Framework
Written by on July 30th, 2008 in Uncategorized.
In my other life as a desktop application developer (which due to a mix of Fluid, AIR, Prism, canvas, SVG, and Flash is threatening to converge on my Ajax life) I’ve long been a fan of data-binding frameworks that make it easy to have a form automatically synchronize with backing data structures, saving you from the tedium of a dozen little “widget.getValue() - dataModel.setValue()” calls (or in the case of grids, etc. much more verbose and tedious plumbing).
Dojo and others frameworks have some interesting binding features, but if your favorite JavaScript framework lacks form data binding, check out Steven Bazyl’s small stand-alone JSON binding project: js-binding.
The project is just getting started, but it already has a few basic features that make it useful. For example, to convert this form:
into this object:
-
-
{
-
username: “…”,
-
email: “…”,
-
address: {
-
street: “…”,
-
city: “…”,
-
state: “…”
-
}
-
}
-
You just need to write this code:
-
-
var myObject = …;
-
var myForm = …;
-
var binder = Binder.FormBinder.bind( myForm );
-
binder.deserialize( myObject );
-
js-binder also has a built-in type conversion mechanism that, for example, allows you to easily integrate with a JavaScript date parsing library:
-
-
var binder = Binder.FormBinder.bind( myForm, {
-
date: {
-
// Date handler using datejs much improved parsing…
-
parse: function( value ) { return Date.parse( value ); },
-
format: function( value ) { return Date.toString( ‘M/d/yyyy’ ); }
-
}
-
} );
-
binder.deserialize( myObject );
-
The docs are concise but useful.
Source: Ajaxian » Front Page
Original Article: http://feeds.feedburner.com/~r/ajaxian/~3/350566132/light-weight-json-binding-framework