MooTools 1.2 Class Template
Starting a MooTools class can be difficult if you haven't created one before. Here's a template you can copy, paste, and use to create your MooTools classes.
MooTools 1.2 Class Template
var yourClass = new Class({
//implements
Implements: [Options],
//options
options: {
yourOption: ''
},
//initialization
initialize: function(options) {
//set options
this.setOptions(options);
},
//a method that does whatever you want
yourMethod: function() {
}
});
MooTools 1.2 Class Usage Template
//once the DOM is ready
window.addEvent('domready', function() {
var yourInstance = new yourClass({
yourOption: 'yourValue'
});
}
![CSS 3D Folding Animation]()
Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...
![6 Things You Didn’t Know About Firefox OS]()
Firefox OS is all over the tech news and for good reason: Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript. Firefox OS has been rapidly improving...
![MooTools ASCII Art]()
I didn't realize that I truly was a nerd until I could admit to myself that ASCII art was better than the pieces Picasso, Monet, or Van Gogh could create. ASCII art is unmatched in its beauty, simplicity, and ... OK, well, I'm being ridiculous; ASCII...
![Fix Anchor URLs Using MooTools 1.2]()
The administrative control panel I build for my customers features FCKEditor, a powerful WYSIWYG editor that allows the customer to add links, bold text, create ordered lists, and so on. I provide training and documentation to the customers but many times they simply forget to...
Great reference point, thanks!
This is something I’ve been curious about for a while with MooTools. What would you use a class for? What determines whether you use a Class or just a function?
I’m really trying to learn best practices for coding with MooTools and other libraries but I haven’t really found an all encompassing resource…all though this site is quickly becoming a favorite.
I’ve been doing the same thing!
@Seth, the best explanation I’ve heard is that classes help make your code more readable, reusable, and less complex. That’s the short answer ;)
@Seth: I use classes when I’m creating functionality with options. I also use classes so I can reference objects. It does keep the code cleaner too!
I use classes when I want to reuse the code with other occasions – in same project, or other – with different options
I use functions when I do more specific tasks, or more simple that a class would be just overhead…
An extra nice post, David. Thanks! (It’s Stumble time!)
Very nice snippet
What about add Extends?
@Imzyos: I don’t extend other classes much.
@Crispjin: Instead of
e.addEvent('mouseenter',function() { });
, you may want to consider adding amouseenter
event to the class and replace the above withthis.fireEvent('mouseenter');
.I’m not very familiar with the JavaScript/moo syntax so I’m still struggling with your suggestion with the this.fireEvent
How do I implement this? Where to I leave the “e” variable witch points to the element I would like to change the opacity? The mootools docs couldn’t give me a right example…
Could you give me a kick in the right direction? Thanks anyway!
Crispijn,
The Netherlands
Thanks for this. For some reason I’ve been using it quite a lot. Simple things are usually best!
One thing I noticed is that Dom Ready is missing ); at the end. So you could add that so it does not confuse newcomers with errors if they use this.
I have been using mootools for a little bit, and I really like it.
Thanks for the tutorials, they’re great… Keep them coming.
Samuel M.
Buensimo el ejemplo, sin embargo los métodos no se disparan a menos que agregues
this.fireEvent('yourMethod');
donde"yourMethod"
es el nombre de tu evento.Ejemplo.-
Esto es útil cuando quieres agregar codigo personalizado al ejecutar dicho evento.
Hi,
This is the tutorial what I am looking for.
Before some days I have tried to contact you regarding this point. but no reply from your side.
But after all thanks for this post.
Thanks
Avi