{"id":4,"date":"2007-08-07T16:05:00","date_gmt":"2007-08-07T08:05:00","guid":{"rendered":"http:\/\/127.0.0.1\/website_linux\/blog\/?p=4"},"modified":"2007-08-07T16:05:00","modified_gmt":"2007-08-07T08:05:00","slug":"low-pro-unobtrusive-scripting-for-prototype","status":"publish","type":"post","link":"https:\/\/opgogo.com\/blog2\/?p=4","title":{"rendered":"Low Pro: Unobtrusive Scripting For Prototype"},"content":{"rendered":"<p>http:\/\/www.danwebb.net\/2006\/9\/3\/low-pro-unobtrusive-scripting-for-prototype<\/p>\n<p>Low Pro is a set of extensions to the Prototype library that make it easier to implement unobtrusive DOM scripting and forms the JavaScript component of the Unobtrusive JavaScript Plugin For Rails but can be used separately. It\u2019s essentially a compilation of various tried and tested, previously published techniques adapted for use with Prototype and mainly centres around enhancing event handling and DOM manipulation. Low Pro uses portions of code and\/or inspiration from Dean Edwards, Matthias Miller, Sylvian Zimmer, Justin Palmer and John Resig.<\/p>\n<p>To start out with Low Pro, download the 0.2 distribution from my Subversion repository and included it along with the Prototype JavaScript file. Low Pro depends on Prototype so don\u2019t forget to include it first:<\/p>\n<p>&lt;script src=&#8221;\/js\/prototype.js&#8221; type=&#8221;text\/javascipt&#8221;&gt;&lt;\/script&gt;&lt;script src=&#8221;\/js\/lowpro.js&#8221; type=&#8221;text\/javascipt&#8221;&gt;&lt;\/script&gt;<br \/>\nIn the distribution is a standard commented version and a compressed version. Now for a quick tour of the features.<\/p>\n<p>DOM navigation and manipulation<br \/>\nAs Prototype\u2019s DOM manipulation is very innerHTML centred Low Pro adds few useful methods to elements to help you navigate and manipulate the DOM in the nice clean W3C way.<\/p>\n<p>$(&#39;doomed&#39;).remove(); \/\/ removes the element from the DOM and returns it$(&#39;item&#39;).nextElement(); \/\/ returns the next element in the document (excluding text nodes)$(&#39;item&#39;).previousElement(); \/\/ guess&#8230;$(&#39;thing&#39;).ins&#101;rtAfter(element); \/\/ the DOM gives you ins&#101;rtBefore but not this$(&#39;bong&#39;).replaceElement(element); \/\/ replaces an element and returns the substitute<br \/>\nYou can get to all of these methods via the DOM object as well.<\/p>\n<p>DOM.remove(element);DOM.ins&#101;rtAfter(oneElement, anotherElement);<br \/>\nAlso, Low Pro aids you in creating node structures via the DOM with a Prototype version of DOM Builder. Each HTML tag has a builder function called $[tag]. You can nest these to cr&#101;ate complex DOM structures easily.<\/p>\n<p>var listItem = $li({ id : &#39;item-1&#39; },                  $strong(&#8220;Some text&#8221;)               ); \/\/ returns a node equivilent to: &lt;li id=&#8221;item-1&#8243;&gt;&lt;strong&gt;Some text&lt;\/strong&gt;&lt;\/li&gt;$(&#39;a_list&#39;).appendChild(listItem);<br \/>\nRead more about DOM builder over at The Web\u2019s Bollocks.<\/p>\n<p>Events<br \/>\nThe event handling code in Prototype is one of it\u2019s weakest points so Low Pro replaces out Event.observe() and Event.stopObserving() totally with a version of Dean Edwards\u2019 addEvent() and removeEvent() routines. This offers much more consistency across browsers and a few neat fixes:<\/p>\n<p>Event.observe(&#39;thing&#39;, &#39;click&#39;, function(e) {  this.hide()  \/\/ this refers to the triggering element.  return false; \/\/ stops the default behaviour (even in Safari!)});<br \/>\nAll other methods remain backwards compatible. For convenience, observe() and stopObserving() are mixed in to elements. A trigger method is also provided to allow you to trigger event handlers programmatically.<\/p>\n<p>Event.trigger(element, &#39;click&#39;);<br \/>\nEvent.onReady() allows you to stack up callbacks that will trigger as soon as the DOM is ready rather than onload which triggers after the whole page is loaded.<\/p>\n<p>Event.onReady(function() {  $(&#39;thing&#39;).visualEffect(&#39;highlight&#39;);});<br \/>\nRepeated calls will simply stack up callbacks.<\/p>\n<p>Behaviours<br \/>\nLow Pro adds declaritive behaviours to Prototype. Event.addBehavior() allows you to specify element behaviours via CSS sel&#101;ctors.<\/p>\n<p>Event.addBehavior({  &#39;a.todo:click&#39; : function(e) {    new Ajax.Request(&#39;todo\/add&#39;, &#8230; );  },  &#39;div.feature:mouseover&#39; : function(e) {    this.hide();  }});<br \/>\nThese behaviours are applied as soon as the DOM is loaded and are reapplied to new elements after Ajax calls. An alt&#101;rnative more OO approach is to cr&#101;ate a Behaviour \u2018class\u2019 which can be attached to elements. Each element gets it\u2019s own instance of the behaviour class that retains it\u2019s state throughout the life of the page. Within behaviour classes, this.element always points to the attached element.<\/p>\n<p>var Resize = Behavior.cr&#101;ate({  initialize : function() {    \/\/ gets called when the element is loaded.  },  onclick : function(e) {    \/\/ use on methods to set event handlers on a object  }});<br \/>\nFinally, I\u2019ve included Sylvian Zimmer\u2019s optimisation of the $$ sel&#101;ctor which speeds up node sel&#101;ctions quite dramatically. It is, in my experience, a little bit buggy though so if you encounter problems you can revert to the normall $$ code.<\/p>\n<p>LowPro.optimize$$ = false;<br \/>\nSo, that\u2019s it. Any feature suggestions are much appreciated as are bug reports. Please log them on my Trac and I\u2019ll fix them ASAP.<\/p>\n<p>Up&#100;ate: Low Pro is compatible with any of the Prototype 1.5 releases but will not work with 1.4 o&#114; below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/www.danwebb.net\/2006\/9\/3\/low-pro-unobtrusive-scripting-for-prototype Low Pro is a set of exte&hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[28],"tags":[],"class_list":["post-4","post","type-post","status-publish","format-standard","hentry","category-computer"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/posts\/4","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4"}],"version-history":[{"count":0,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/posts\/4\/revisions"}],"wp:attachment":[{"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}