{"id":479,"date":"2012-06-01T10:06:50","date_gmt":"2012-06-01T14:06:50","guid":{"rendered":"https:\/\/www.goer.org\/wordpress\/why_does_yui_have_y_one_method"},"modified":"2013-11-17T06:33:51","modified_gmt":"2013-11-17T06:33:51","slug":"why_does_yui_have_y_one_method","status":"publish","type":"post","link":"https:\/\/www.goer.org\/Journal\/2012\/06\/why_does_yui_have_y_one_method.html","title":{"rendered":"Why Does YUI Have a Y.one() Method?"},"content":{"rendered":"<p>When I was working on the <a href=\"\/YUI\/\">YUI 3 Cookbook<\/a>, 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&#8217;s-mouth account of YUI&#8217;s architecture and design. With the <a href=\"http:\/\/shop.oreilly.com\/product\/0636920013303.do\">release<\/a> <a href=\"http:\/\/itunes.apple.com\/us\/book\/yui-3-cookbook\/id530503625?mt=11\">of the<\/a> <a href=\"http:\/\/www.amazon.com\/YUI-3-Cookbook-ebook\/dp\/B0086I5M4W\/ref=kinw_dp_ke?ie=UTF8&amp;m=AG56TWVU5XWC2\">cookbook<\/a>, 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.<\/p>\n<p>One basic thing you might be wondering about is&#8230; why does YUI have <em>two<\/em> idioms for reaching into the DOM?<\/p>\n<p>YUI&#8217;s first method, <code>Y.all()<\/code>, retrieves a <code>Y.NodeList<\/code> instance:<\/p>\n<pre><code>var errorDivs = Y.all('div.error');\n<\/code><\/pre>\n<p>If the selector fails to match any elements, <code>Y.all()<\/code> returns an empty <code>Y.NodeList<\/code>.<\/p>\n<p>By contrast, the second method, <code>Y.one()<\/code>, retrieves a single <code>Y.Node<\/code> instance:<\/p>\n<pre><code>var errorDiv = Y.one('div.error');\n<\/code><\/pre>\n<p>If the selector matches multiple nodes, <code>Y.one()<\/code> returns the first match (as if you were using the <code>:first<\/code> jQuery pseudo-selector). If the selector fails to match any elements, <code>Y.one()<\/code> returns <code>null<\/code>.<\/p>\n<p>So what&#8217;s up with this? Why bother having two methods, and why do they behave so differently? Why not just have <code>querySelectorAll()<\/code>-like behavior and be done with it?<\/p>\n<p>The answer is that it&#8217;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 <code>Y.one()<\/code> returns <code>null<\/code>, it is great for simple node existence checks:<\/p>\n<pre><code>if (Y.one('#myDiv')) {\n    ...\n}\n<\/code><\/pre>\n<p>As for <code>Y.all()<\/code>, 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:<\/p>\n<pre><code>Y.all('div.error').remove();\n<\/code><\/pre>\n<p>Some libraries rely on a single abstraction (reaching into the DOM always returns a collection), but the choice of <code>Y.all()<\/code> and <code>Y.one()<\/code> &#8212; or, if you&#8217;re thinking in native terms, <code>querySelectorAll()<\/code> and <code>querySelector()<\/code> &#8212; ultimately enables you to write cleaner code. The cost is having an extra API method to think about. Fortunately, YUI&#8217;s methods have short, distinct names, so it&#8217;s pretty easy to remember which one does what.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The answer is that it&#8217;s helpful to know ahead of time whether you will receive a single node or a collection. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-479","post","type-post","status-publish","format-standard","hentry","category-web"],"_links":{"self":[{"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/posts\/479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/comments?post=479"}],"version-history":[{"count":3,"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/posts\/479\/revisions"}],"predecessor-version":[{"id":574,"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/posts\/479\/revisions\/574"}],"wp:attachment":[{"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/media?parent=479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/categories?post=479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/tags?post=479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}