mraj (104) [Avatar] Offline
#1
Some style comments/questions for Photomatic:

1. Since photoElement and nextControl both perform the same operation (advance to the next slide), the click event handlers can be combined, using add() to create the wrapped set with both elements:

settings.photoElement.add(settings.nextControl).click(function() {
showPhoto((settings.current+1) % settings.thumbnails.length);
});

If the above looks strange, consider that the following also works, but it is slower, since we'd be creating the photoElement wrapped set again (which is already stored in variable settings.photoElement):

$(settings.photoElement).add(settings.nextControl).click(function() {

At first I tried
$(settings.photoElement, settings.nextControl)
thinking I was using the "multiple selectors" format (http://docs.jquery.com/Selectors/multiple#selector1selector2selectorN), but it turns out that what I wrote is using the "contextualized selector" format (http://docs.jquery.com/Core/jQuery#elements).

The multiple selector format would need to take one argument (not two)... something like $("#photo, #nextButton"), but I don't know how to write that using (the settings) variables, so I used add() instead.

2. Why
var showPhoto = function(index) {
instead of
function showPhoto(index) {
? Is this to remind us that "functions are not named entities" (p. 327)?

3. p.213, it might be helpful to remind readers that the wrapped set is being treated like an array here (as discussed on p. 35):
settings.transformer(settings.thumbnails[index].src));
instead of like a wrapped set:
settings.transformer(settings.thumbnails.get(index).src));

This was a great example. I learned much from it.
bear.bibeault (675) [Avatar] Offline
#2
Re: Chapter 7, photomatic, style questions
I'm glad that you liked the example, and it's great to see your suggestions for improving it. Shows that you did, indeed, learn much from it.

And yes, sometimes example code isn't as efficient as it could be in order to really drive home a concept, or to learn a specific lesson. I get annoyed with books that feel that their code always has to be the most "clever" at the expense of understanding the concepts.

You'll be happy to hear that the Photomatic will continue to be included in the 2nd edition, albeit with a new look, and a brand new implementation that makes use of some of the jQuery advances since the first edition.