MVC pattern with YUI reviewed

I updated the Temperature Example, described in my previous post, using YAHOO.util.AttributeProvider.

Now the example is quite far away the original one. The legacy code for the MVC pattern is gone: AttributeProvider and Attribute substitute GenericDocument and DocumentItem at all.

It seems YUI support natively the MVC pattern.

I modified the document, TemperatureDocument: now it inherites from AttributeProvider.

Then I modified the view, TemperatureView.

setTemp() calls the conversion routines and updates the fields, firing the change events. Truly, these lines are not changed.

setAttributeConfig() adds the attribute to the model. In the previous code it was done by get() call, creating the attribute automagically if it didn't exist.

Then, after retrieving the attribute from the document model, I add an event handler: the event is created on the fly. Remember to add 'Change' string to the event, because setValue() call of the attribute will fire an event called with name plus 'Change'.

I added the check between previous and new value to stop circular (and infinite) field updating.

Books to read

JavaScript: The Good Parts © 2008 by Douglas Crockford

The Future of Management © 2007 by Gary Hamel, Bill Breen

Pro JavaScript Design Patterns © 2007 by Ross Harmes, Dustin Diaz

The Search © 2005 by John Battelle

Joel on Software © 2004 by Joel Spolsky

Entanglement: The Greatest Mystery in Physics © 2001 by Amir D. Aczel

Minds, Machines, and the Multiverse. The Quest for the Quantum Computer © 2000 by Julian Brown

Black Holes, Wormholes & Time Machines © 1999 by Jim Al-Khalili