Archive for the ‘YUI’ Category

Instance Manager Object

Sunday, January 25th, 2009

I have been using a new pattern for managing instances of JavaScript widgets in applications. I define a widget as any component in an app that is JavaScript enhanced. This could be anything from a DataTable to a button.

In the YUI 2X idiom of interface development, you would create a namespace for a section of your application. All of your instances, constructors, and various other junk would live in the scope of this namespace. My instance manager pattern would slightly change this in that constructed instances of widgets would be stored in a Instance Manager object. This object would be constructed as a member of any namespace that would be home to an instance of a widget. All of these instances would be kept in a sub-object of the Instance Manager which would be known as the “map” object.

The Instance Manager object would have 3 more methods:

  • get
  • set
  • delete

“set” method
The “set” method that can be used to add an instance to the “map” object. Passing the constructor as an argument instead of passing an instance will delay the instantiation until the first time the instance id is called from the “get” method this is known as lazy loading.

“get” method
The “get” method that can be used to return an instance from the map object by passing its key as an argument.

“delete” method
The “delete” method will remove the instance from the map object by passing its key as an argument.

The point of this pattern is to create a consistent way to create and get instances of widgets. This is especially useful for use with the Unobtrusive JavaScript pattern because code written to look for special markup conventions on page load can be generalized