loader for custom mootools classes

For posterity, I thought I’d post code I was using to initialize multiple instances of some of my mootools classes with default and DOM element specific options. Additionally, I only wanted a class to be initialized for an element actually on the page, so #filter is used.

Mootools is definitely more fun than Ext JS. If mootools had a polished component library like Ext JS, I’d still be using it. Plus, I prefer the Mootools license. Pragmatism won the day.

function loader(klass, options, defaults) {
        $H(options).filter(function(options, el) {
                if($(el)) { return true; }
        }).each(function(options, el) {
                new klass(el, (($type(defaults) == 'object') ? $extend(options, defaults) : options));
        });
}

And usage. DatePicker is from the cnet mootools collection.

loader(DatePicker, {
        conversation_discussed_at:{
                stickyWinOptions:{offset:{x:-100,y:10}}, // Doesn't fit on screen!
                additionalShowLinks:['conversation_discussed_at_img']
                }
        },
        {format:'%x %X', time: true}
);

Post a Comment

Your email is never shared. Required fields are marked *

*
*