Web development , php , ajax , symfony, framework, zend
In: web resources
18 Jan 2010A while back I shared a few MooTools class creation tips that I live by. More experience in creating MooTools plugins has given me some new guidelines to share.
The number one tip I give to MooTools Class rookies is this: create the desired class functionality before turning it into a class. Once you have the code working using “inline” MooTools javascript, evaluate what you have. You may decide that your functionality is too specific to the task at hand that making a Class isn’t the right choice.
If you do think you should turn the code into a class, take a look at all of your initial “var {key} = {value}” declarations — those will likely be your arguments and options. Take any repeated code and turn them into class methods. Do the same for specific functionalities the Class should have. Here’s a very basic example:
window.addEvent('domready',function() {
/* settings */
var showDuration = 3000;
var container = $('slideshow-container');
var images = container.getElements('img');
var currentIndex = 0;
var interval;
var toc = [];
var tocWidth = 20;
var tocActive = 'toc-active';
/* new: starts the show */
var start = function() { interval = show.periodical(showDuration); };
var stop = function() { $clear(interval); };
/* worker */
var show = function(to) {
images[currentIndex].fade('out');
toc[currentIndex].removeClass(tocActive);
images[currentIndex = ($defined(to) ? to : (currentIndex < images.length - 1 ? currentIndex+1 : 0))].fade('in');
toc[currentIndex].addClass(tocActive);
};
/* new: control: table of contents */
images.each(function(img,i){
toc.push(new Element('a',{
text: i+1,
href: '#',
'class': 'toc' + (i == 0 ? ' ' + tocActive : ''),
events: {
click: function(e) {
if(e) e.stop();
stop();
show(i);
}
},
styles: {
left: ((i + 1) * (tocWidth + 10))
}
}).inject(container));
if(i > 0) { img.set('opacity',0); }
});
/* new: control: next and previous */
var next = new Element('a',{
href: '#',
id: 'next',
text: '>>',
events: {
click: function(e) {
if(e) e.stop();
stop(); show();
}
}
}).inject(container);
var previous = new Element('a',{
href: '#',
id: 'previous',
text: '<<',
events: {
click: function(e) {
if(e) e.stop();
stop(); show(currentIndex != 0 ? currentIndex -1 : images.length-1);
}
}
}).inject(container);
/* new: control: start/stop on mouseover/mouseout */
container.addEvents({
mouseenter: function() { stop(); },
mouseleave: function() { start(); }
});
/* start once the page is finished loading */
window.addEvent('load',function(){
start();
});
});
And now the Class version:
var SimpleSlideshow = new Class({
options: {
showControls: true,
showDuration: 3000,
showTOC: true,
tocWidth: 20,
tocClass: 'toc',
tocActiveClass: 'toc-active'
},
Implements: [Options,Events],
initialize: function(container,elements,options) {
//settings
this.container = $(container);
this.elements = $$(elements);
this.currentIndex = 0;
this.interval = '';
if(this.options.showTOC) this.toc = [];
//assign
this.elements.each(function(el,i){
if(this.options.showTOC) {
this.toc.push(new Element('a',{
text: i+1,
href: '#',
'class': this.options.tocClass + '' + (i == 0 ? ' ' + this.options.tocActiveClass : ''),
events: {
click: function(e) {
if(e) e.stop();
this.stop();
this.show(i);
}.bind(this)
},
styles: {
left: ((i + 1) * (this.options.tocWidth + 10))
}
}).inject(this.container));
}
if(i > 0) el.set('opacity',0);
},this);
//next,previous links
if(this.options.showControls) {
this.createControls();
}
//events
this.container.addEvents({
mouseenter: function() { this.stop(); }.bind(this),
mouseleave: function() { this.start(); }.bind(this)
});
},
show: function(to) {
this.elements[this.currentIndex].fade('out');
if(this.options.showTOC) this.toc[this.currentIndex].removeClass(this.options.tocActiveClass);
this.elements[this.currentIndex = ($defined(to) ? to : (this.currentIndex < this.elements.length - 1 ? this.currentIndex+1 : 0))].fade('in');
if(this.options.showTOC) this.toc[this.currentIndex].addClass(this.options.tocActiveClass);
},
start: function() {
this.interval = this.show.bind(this).periodical(this.options.showDuration);
},
stop: function() {
$clear(this.interval);
},
//"private"
createControls: function() {
var next = new Element('a',{
href: '#',
id: 'next',
text: '>>',
events: {
click: function(e) {
if(e) e.stop();
this.stop();
this.show();
}.bind(this)
}
}).inject(this.container);
var previous = new Element('a',{
href: '#',
id: 'previous',
text: '<<',
events: {
click: function(e) {
if(e) e.stop();
this.stop();
this.show(this.currentIndex != 0 ? this.currentIndex -1 : this.elements.length-1);
}.bind(this)
}
}).inject(this.container);
}
});
See how easy the transition is when you have the inline code in front of you? Simple!
Adding events to your classes gives a completely new level of control over the class functionality. Consider my Overlay class. I spent 95% of the time creating the plugin functionality and 5% implementing events but I consider events essential to the Overlay. Here’s a quick example of implementing events:
//.....
Implements: [Options,Events],
open: function() {
this.fireEvent('open');
//do stuff
},
//.....
Now my Overlay class allows you fire events at 4 different times. More flexible and a perfect usage of events.
Judge your list of options and your class’ primary responsibilities to determine if all of the options should be kept as options and not as arguments, and visa versa. When I come to a 50/50 decision I choose to make the parameter an option. If the class cannot live without the given option/argument and there’s no suitable default, keep it as an argument.
One of the great advantages of using a javascript frameworks and classes is the ability to chain method class to make your code shorter. And implementing that type of functionality is so easy!
//.....
store: function(key,value) {
this.data[key] = value;
return this;
}
//.....
Don’t let your methods end with nothing — return this!
Share your awesome plugin by posting it in the MooTools Forge. Sharing your plugins in the Forge allows you to easily get feedback, improvements, and requests.
There you have it. Feel free to share any of your tips!
Don’t forget to follow me on Twitter and be sure to visit Script & Style for the best Javascript and CSS articles around!
Sponsor the David Walsh Blog and get your brand in front of several thousand users per day!
MooTools Class Creation Tips II
Related posts:
This blog delivers stylish and dynamic news for designers and web-developers on all subjects of design, ranging from: CSS, Ajax, Javascript, web design, graphics, typography, advertising & much more. Our goal is to help you communicate effectively on the web with an engaging website or functional interface.
4 Responses to MooTools Class Creation Tips II
jamiesacademyofdance
January 21st, 2010 at 6:33 am
limber up
Sand
March 22nd, 2010 at 5:49 am
A few days ago I found in Yahoo Q/A, a very appropriate and consistent answer on the same subject matter.
It was a best chosen answer and written by some YAHOO-ANSWERER, a Dr of molecular biology. His answer was (is) not only consistent but also very descriptive and clear.
Please open this link…..
and see the quality of this answer. I think you will be satisfied.
__________________________________
Beside, I can provide you with some more useful links included here underneath……
1. Here is a link with "EVERYTHING AND ALL" about proteins
2. A list of proteins with functions
___________________________________
I think those SPECIAL ENYMES are kinase proteins or PROTEIN-KINASE that controls a chemical reaction and catalyses the reaction. The main target is an another prtein in the reaction but not other molecules involved…
This link may help you to comprehend better
http://www.possibility.com/wiki/index.php?title=ProteinKinase
JODI T
March 22nd, 2010 at 4:25 pm
ME TOO! I don't have an answer but PLEASE if you find anything out, can you post?
Hiei
March 27th, 2010 at 5:44 pm
i ripped it straight from the show and uploaded just for you.
hope i helped