MooTools: Class Methods and Inheritance
In my last project, I enjoyed scripting using mootools and started exploring it more deeply. One of my extensive use was to define UI elements’ functionality in classes and then create multiple instances for re-usability.
Mootools makes use of Javascript’s native prototype model for inheritance. To extend a base functionality into sub class, mootools provide ‘Extends’ method. However, if you have class methods in your base model, Extends is insufficient to get them into the sub classes. After some research, I managed to solve this by using Class Mutators and overriding the Mootools ‘Extends’ to take care of class methods inheritance.
First of all, we need to define a ClassMethods mutator. Mutators belong to a class, not to their instances and they are called only when we define a new Class.
Next, we need to redefine Mootools Extends mutator:
Check out the example below:
You can see the above code working here.
http://jsfiddle.net/rZvJE/11/
Tips: Replace Missing Images using JQuery
Checking for non-existing images and replacing them by placeholders is simple when the images are handled on server (db/filestore). However, for externally linked images, we can do something like this using JQuery:
This script will replace the broken image urls with ‘/images/noimage.jpg’.
JS based easy Field Validations in Rails
Most of you will disagree with this post to use Javascript based form validations in Rails. However this is an attempt to simplify integration of DEXAGOGO - Really easy field validations in Rails. This is one of the most structured and easy validation technique written by Andrew Tetlaw and team. (Demo)
To use it in Rails, copy simple_validation.rb in application lib directory and add following lines to config/environment.rb file.
Download and Import validation.js along with prototype.js in layout from here.
Two methods are introduced to render forms.
- validated_form_for
- validated_remote_form_for
Usage:
:class attribute refers to validation rule (or combination of rules) and :title attribute is the validation message. Details of all rules are available at http://tetlaw.id.au/view/javascript/really-easy-field-validation


