or taking advantage of those extra 100 KB in your page
Let's be honest - I'm tired of answering same questions over and over again. Prototype.js is a widely used javascript library with somewhat confusing online documentation. I personally find API reference to be a great resourse overall (with few glithes of course), but it's far from being newbie-friendly. I've been developing with prototype for almost a year now and have been spending a lot of time on the IRC channel. It's hard to explain what kind of nonsense people are asking sometimes. It seems to me that most of the time prototype is used at 15%, not more. I'm not surprised anymore to see Ajax.Request used with document.getElementById on one line
Update: How well do you know prototype :: part II
Here, I've collected most common use cases that do NOT use all of prototype's capabilities and their simple solutions. I hope this will be a basic checklist to go through when developing for your next project. So... Here we go:
document.getElementById('foo')$('foo')Surprisingly some people actually don't know about this one ( including ~100KB file just to use Ajax.Request family )
var woot = document.getElementById('bar').valuevar woot = $('bar').valuevar woot = $F('bar')Handy shortcut for reading a value of a form control
$('footer').style.height = '100px';
$('footer').style.background = '#ffc';
$('footer').setStyle({
height: '100px',
background: '#ffc'
})
Dreaming about IE behaving W3C way? Not happenning! (but second construct will make you forget about cross-browser glitches)
$('coolestWidgetEver').innerHTML = 'some nifty content'$('coolestWidgetEver').update('some nifty content')
One of those simple ones yet quite often forgotten. Yes, I know they are almost the same but I want to see you doing THIS with the first one
(isn't chaining just cool?)
$('coolestWidgetEver').update('some nifty content').addClassName('highlight').next().hide()
new Ajax.Request('ninja.php?weapon1=foo&weapon2=bar')
new Ajax.Request('ninja.php', {
parameters: {
weapon1: 'foo',
weapon2: 'bar'
}
})
Cleaner and better structured parameters definition
new Ajax.Request('blah.php', {
method: 'POST',
asynchronous: true,
contentType: 'application/x-www-form-urlencoded',
encoding: 'UTF-8',
})
new Ajax.Request('blah.php')
All of these options are in Ajax.Request by default! "method: 'POST'" happens to be on every second pastie page I've seen
(Still don't believe in JS inheritance? You don't have to. Just take advantage of it)
Event.observe('myContainer', 'click', doSomeMagic)
$('myContainer').observe('click', doSomeMagic)
This one is debatable but second way is more Object Oriented (well... sort of) and easier to chain (So decide for yourself)
$$('div.hidden').each(function(el){
el.show();
})
$$('div.hidden').invoke('show')
Here's a typical "each overuse". We have invoke for such things, folks! Sadly not many people know about it.
$$('div.collapsed').each(function(el){
el.observe('click', expand);
})
$$('div.collapsed').invoke('observe', 'click', expand)
Ha! Take this! Invoke can also be used for event handling when iterating over a collection of elements. It's really easy, isn't it?
$$('input.date').invoke('observe', 'focus', onFocus);
$$('input.date').invoke('observe', 'blur', onBlur);
$$('input.date')
.invoke('observe', 'focus', onFocus)
.invoke('observe', 'blur', onBlur)
Somehow people tend to forget about "chaining nirvana". Don't like the way it looks? Think about saving some time by NOT invoking $$ twice!
$('productTable').innerHTML =
$('productTable').innerHTML +
'<tr><td>' + productId + ' '
+ productName + '</td></tr><tr><td>'
+ productId + ' ' + productPrice +
'</td></tr>'
var rowTemplate = new Template('<tr><td>#{id} #{name}</td></tr><tr><td>#{id} #{price}</td></tr>');
$('productTable').insert(
rowTemplate.evaluate({
id: productId,
name: productName,
price: productPrice
}))
)
No I'm not kidding. This has been posted to #prototype and something was wrong with it (hmm... I wonder what)
Templates are great for those nasty HTML strings. Those humongous concatenated constructs make me feel slightly nauseous
evaluate takes care of all the parsing in a nice structured way