On a recent project, part of the scope was to address bugs and add new functionality to an existing script that was at the core of the internal tool I was updating. Although the code worked and had very few issues, it was the farthest from modern design patterns as you could get. The bigger part of the script (~1000+ lines of code) was riddled with global variables/functions, few relationships between objects, and several set/get methods only returning simple values. I appreciate that the logic/flow/functionality was mostly figured out by the time I got my dirty little hands on it but it was in serious need of an overhaul.

As I went along refactoring I realized the relationship between the multiple forms and the objects they represent could be handled a little better. As a result, this tiny fella was born. I love it when patterns just emerge. It is a general setter/getter object with focus on keeping a form’s values and it’s object(s) values the same. It has the following features:

  • Prototyped by array so it’s somewhat of an array/hash hybrid
  • When objects are prototyped by this object they can be managed as single objects or an array of objects
  • Beginnings of basic value validation
  • Supports child objects that need to perform post setting routines or special setting routines per property
  • Create a two-way relationship between the object properties and form values that almost entirely rely on a simple element/property naming convention. When you change a form value, the object property is updated. When you change an object property, the form value is updated. At every turn, the (very) simple validation is performed.
  • Show the code