Why Does YUI Have a Y.one() Method?

When I was working on the YUI 3 Cookbook, one of the things I really enjoyed was being able to pick the brains of the YUI team and get a straight-from-the-horse’s-mouth account of YUI’s architecture and design. With the release of the cookbook, I thought it would be fun to look back through the manuscript and pick out some short topics that might be of interest to new and intermediate YUI users.

One basic thing you might be wondering about is… why does YUI have two idioms for reaching into the DOM?

YUI’s first method, Y.all(), retrieves a Y.NodeList instance:

var errorDivs = Y.all('div.error');

If the selector fails to match any elements, Y.all() returns an empty Y.NodeList.

By contrast, the second method, Y.one(), retrieves a single Y.Node instance:

var errorDiv = Y.one('div.error');

If the selector matches multiple nodes, Y.one() returns the first match (as if you were using the :first jQuery pseudo-selector). If the selector fails to match any elements, Y.one() returns null.

So what’s up with this? Why bother having two methods, and why do they behave so differently? Why not just have querySelectorAll()-like behavior and be done with it?

The answer is that it’s helpful to know ahead of time whether you will receive a single node or a collection. This in turn is why the two methods return different values when the selector fails to find a match. Since Y.one() returns null, it is great for simple node existence checks:

if (Y.one('#myDiv')) {
    ...
}

As for Y.all(), the fact that it always returns an empty collection makes it always safe for doing bulk operations. For instance, the following works just fine even if there are no error divs in the document:

Y.all('div.error').remove();

Some libraries rely on a single abstraction (reaching into the DOM always returns a collection), but the choice of Y.all() and Y.one() — or, if you’re thinking in native terms, querySelectorAll() and querySelector() — ultimately enables you to write cleaner code. The cost is having an extra API method to think about. Fortunately, YUI’s methods have short, distinct names, so it’s pretty easy to remember which one does what.

Posted in Web

YUI 3 Cookbook Now Available in DRM-free EPUB and MOBI

Building on last week’s news, the YUI 3 Cookbook is now officially available for purchase as an ebook! So far I’ve seen it available at the following fine Universal Resource Locations:

The multiformat bundle from O’Reilly is definitely DRM-free. I am pretty sure the Apple and Amazon files are also DRM-free… because if they weren’t, that would be just insanely stupid.

It goes without saying that yes, the YUI 3 Cookbook is also available in print form. You know, if you hate trees. When you were in school, and they told you that if you were lost, you should hug a tree — didn’t you think that was just a little bit creepy? You and me both, kid.

Posted in Web

YUI 3 Cookbook is off to the Printer!

I’m pleased to announce that the YUI 3 Cookbook is officially off to the printer! Hooray! Unfortunately I’m not actually at O’Reilly Tower right now, but I do have exclusive footage of the printing process getting underway:

You can still pre-order the physical YUI 3 Cookbook through O’Reilly Media, Barnes & Noble, Amazon.com, Powell’s Books, and many other fine vendors. Also: if for some reason you actually like trees, my spies report that ebooks might be available as early as next week! Subscribe to this blog for future updates.

The Corporation as Angel

Much like you, I’ve never had much truck with the kitschy view of angels.

My favorite take on angels comes from The Prophecy. The Prophecy is a rather kitschy movie itself, but it deserves real credit for portraying angels that behave like they come from the Pentateuch, not a Hallmark card:

Recently I just finished reading David Graeber‘s excellent Debt: The First 5,000 Years. One of his asides caught my attention:

[Corporations are] entities that, through a charming legal fiction, we imagine to be persons, just like human beings, but immortal, never having to go through all the human untidiness of marriage, reproduction, infirmity, and death. To put it in properly Medieval terms, they are very much like angels.

Legally, our notion of the corporation is very much a product of the European High Middle Ages…

As an abstraction for thinking about corporations, “angelhood” seems like a much better fit than “personhood.” What are angels? They are alien. They are immortal, and hard to kill. They ruthlessly pursue their goals. They don’t feel pity, or remorse, or fear (though sometimes, they can be bargained or reasoned with).

In Hebrew, the word for angel is Malakh, “messenger.” This rings true as well, as modern corporations are absolutely obsessed with their “message.” The wrinkle is that unlike Biblical angels, the message corporations want to give you is never the message that they are actually carrying. This is what makes wrestling with them so complicated.

Backscatter Spam Attack from orange.fr

I’m currently suffering a backscatter spam attack, 95% of which is coming from orange.fr’s misconfigured email servers. 500 emails and counting. I’ve certainly seen worse (much worse), but not anything on this scale for many years. It feels kind of… retro.

Presumably the reason backscatter spam has grown rarer is that these days, most mail servers are now configured properly. And yet — it seems that at end of 2011, there are still major service providers that employ systems engineers and product managers who do not understand the basic principles of mail server configuration.

So help educate the fine people who work for orange.fr, here is an article that explains why you should never bounce spam and viruses. And here is the French translation of the same article.

You’re welcome, orange.fr systems engineers and product managers! I’m glad we were able to have this positive cultural exchange.

The Halakha of Amazon Price Scanning

Reading “Amazon’s Jungle Logic” reminded me of a lecture I heard from a rabbi years ago, about the ethics of entering a shop to examine an item without any intention of making a purchase. Longtime readers of the Talmud (or even people casually aware of the Talmud) might not be surprised to learn that the Talmud has something specific to say about this situation. The Mishnah draws an interesting analogy between hurtful business practices and hurtful words, saying:

Just as there is overreaching in buying and selling, so is there wrong done by words. Thus, one must not ask another, “What is the price of this article?” when one has no intention of buying. If a person was a repentant sinner, one must not say to him, “Remember your former deeds…”

Here, “overreaching in buying and selling” means “overcharging”, or more generally, business fraud. The rabbis have plenty to say about business fraud in the Gemara. (They have plenty to say about everything in the Gemara.)

Some contemporary readings of this Mishnah argue that the underlying reason this behavior is bad is that it is unethical to waste a merchant’s time or falsely raise their hope. Under that reading, if you swoop in, scan some items, and then rush out of the store again to collect your five bucks, it’s hard to argue that you wasted that much of anybody’s time. Particularly if you walk fast, avoid eye contact, and above all, avoid talking to anybody — for most smartphone users, par for the course.

However, at least from what I can find in the Talmud’s sections on business practices — and I certainly might have missed a passage — I’m not sure that the “wasting the merchant’s time and energy” argument is really rooted in the text. As far as I can tell, the real problem stems from fraudulence. Or as R. Judah puts it, “One may also not feign interest in a purchase when he has no money, since this is known to the heart only, and of everything known only to the heart it is written, and thou shalt fear thy God.” (Meaning: you can fool the storekeeper, but you can’t fool God.)

Under that reading, I think the real question is, does price scanning with zero intent to purchase carry the whiff of fraudulence? I propose the following moral Gedankenexperimente. Imagine you walk into the store this Saturday, smartphone hot in your hand. A clerk stops you and asks if there’s anything he or she can help you with.

If you can look the clerk in the eye and respond with, “No thanks, I’m fine — I’m just heading over to scan this item with this Amazon app, and then I’ll clear right out,” with no hesitation or mumbling or red-facedness, then congratulations! Your heart and actions are aligned. Go ye forth and scan. Otherwise, consider taking a pass.

Of course, it’s worth mentioning that participating in Amazon’s particular little game this holiday season means running off to engage in commerce on a Saturday, which is super-duper illegal no matter how you slice it. I mean, at that point you might as well make sure that your Sabbath day price-scanning shenanigans involve some kind of bacon-related product. Go for the trifecta!

The YUI 3 Cookbook Draft is Done

For certain values of “done.”

  • Quite honestly, it still could use a couple of Loader recipes.
  • And at least one more i18n recipe.
  • Not to mention Luke Smith totally stole my tripleclick synthetic event recipe and did it better, so I need to redo that one from scratch.
  • Plus I need to update the App Framework recipes for 3.5.0.
  • Ditto for most of the Node.js recipes.
  • Above all, I should really give it a global editing pass before it gets to the editor and copy editor.

Yes, but aside from Loader, i18n, synthetic events, the App Framework, Node.js, and a global editing pass, the draft is done!

  • Maybe a FocusManager recipe.

Free YUI 3 Cookbook Sample Pack (Updated x2)

With the YUI 3 Cookbook draft nearing completion, I’ve put out a PDF sample pack of the first three chapters. The sample pack was actually announced last week during the YUIConf 2011 keynote, but here it is again:

TOC and Sample Pack

YUI 3 Cookbook Sample Pack

  1. Loading YUI
  2. DOM Manipulation
  3. UI Effects and Interactions
  4. Events
  5. AJAX
  6. CSS
  7. Infrastructure
  8. Widgets
  9. Utilities
  10. Server-side YUI
  11. Universal Access
  12. Professional Tools

CAUTION: Cookbook Sample Pack has not gone through the O’Reilly editing and technical review process. Cookbook Sample Pack contains numerous typos, awkward phrases, stylistic inconsistencies, and flat-out technical errors. Discontinue use of Cookbook Sample Pack if any of the following occurs: itching, tingling in extremities, loss of balance or coordination, or slurred speech. Do not taunt Cookbook Sample Pack.

Update #1: Bonus Material

By the way, if you downloaded the sample pack last week, please download it again. The updated version includes a number of edits I had waiting in the wings, plus some great catches from @codertales, plus TWO BONUS recipes, one about the Resize plugin, the other how to register custom named transitions for easy reuse. Whew! So what are you waiting for? Go get it! But mind the itching.

Update #2: February 22, 2012

The YUI 3 Cookbook Sample Pack has been updated again, this time incorporating a large number of technical edits. Still not formally copyedited, but other than that, this is as good as it gets!

Posted in Web

The Blog is 10 Years Old Today

October 26, 2001 was the first blog post I ever made. It was handcrafted plain HTML. Eventually I discovered there was such a thing as blogging software, and then it was off to the races.

It takes a special kind of thick-headedness these days to keep producing medium-sized articles, with an Atom feed, on your own domain. To celebrate this thick-headedness, here are ten posts of note, one for each year.

I can’t claim that any given post is the best of that year, just that it was the best I could find on short notice. Will try to do better next decade.