Activate your free membership today | Log-in

Wednesday, May 7th, 2008

Lazily load functionality via Unobtrusive Scripts

Category: JavaScript, Prototype

David Kees has written about Using Prototype to Load Javascript Files, which is an implementation of the general technique of loading functionality via scripts based on the availability of DOM elements.

He started using the technique to scratch an itch:

The calendar on this site only appears on pages that show blog-related information. That calendar is enhanced with Javascript allowing you to change the month displayed by the calendar without reloading the rest of the page. So, in order to ensure that these enhancements would be available everywhere the calendar is, I figured I had two options:

  1. Code the inclusion of the Javascript file into every page which requires it. While a good solution, and one that has worked well in the past, sometimes it can be difficult to remember or realize that you've forgotten to include the file when you add new pages that require it.
  2. Find a way to automatically include the file when it's needed. This would avoid the need to remember the need to include it when adding new pages that require it, but would result in a little more Javascript going on when the page loads.

Here is an example implementation:

JAVASCRIPT:
  1.  
  2. document.observe("dom:loaded", function() {
  3.         var calendar = $("calendar");
  4.         if(calendar) {
  5.                 var script = new Element("script", { type: "text/javascript", src: "/path/to/calendar.js" });
  6.                 document.observe("calendar:loaded", function() { new Calendar(calendar); });
  7.                 $$("head")[0].insert(script);
  8.         }
  9. });
  10.  

Posted by Dion Almaer at 1:02 pm
14 Comments

++++-
4 rating from 9 votes

Monday, May 5th, 2008

Ajax Pioneer Week: Sam Stephenson of Prototype

Category: Prototype, Interview, Podcast

We are having a special week at Ajaxian. Ben and I are giving an Ajax talk at JavaOne this week, and decided to put a little video from Ajax pioneers. As we worked out what we wanted to do, we asked the pioneers for a little time to do an interview. Although only a piece of the interview will be used in the live presentation, we wanted to get the full interviews for the community here.

During the week you will hear from:

  • Sam Stephenson of Prototype
  • Bruce Johnson of GWT
  • Alex Russell of Dojo
  • John Resig of jQuery

On Wednesday, we will have a special video that features Ben and I having some fun with a new type of Ajax application.

Let's cut to the chase, and listen in to Sam Stephenson. Although we couldn't get to him in person, he kindly recorded himself via his laptop. My voice quality is poor, but we are all hear to listen to his thoughts on:

  • The future of Prototype
  • What excites him about new versions of Prototype, and what problems are they trying to solve
  • Thoughts on the current crop of browsers, and what he wants to see

In the interview he discusses pdoc, a new inline documentation tool, Sprockets, a tool to help package Prototype, and new event delegation techniques.


Posted by Dion Almaer at 11:16 am
14 Comments

++++-
4.4 rating from 30 votes

Tuesday, April 29th, 2008

Twistori: Telling a story with Tweets and Script.aculo.us

Category: Prototype, Showcase, Scriptaculous

Twistori

Twistori is a fun little site created by Amy Hoy and Thomas Fuchs. As you would expect, design is a key part of the application, and the Prototype / Script.aculo.us combo pull off the work.

The site pulls in live data on various topics (love, hate, think, believe, feel, wish) via the real-time twitter search tool summize.

In related Twitter news, I created a Greasemonkey script Twitter Translate that auto-translates foreign text to your language inside Twitter.

For more Ajax news, you can follow me @dalmaer or our new @ajaxian account.

Posted by Dion Almaer at 7:08 am
4 Comments

++++-
4.1 rating from 40 votes

Wednesday, April 23rd, 2008

Timeframe: Prototype date range component

Category: JavaScript, Library, Component, Prototype

Stephen Celis got tired of wiring together two date pickers for the common use case of grabbing a date range, so he created timeframe, which is "Click-draggable. Range-makeable. A better calendar."

Based on Prototype, you can whip up some code such as:

JAVASCRIPT:
  1.  
  2. new Timeframe('calendars', {
  3.             startfield: 'start',
  4.             endfield: 'end',
  5.             previousbutton: 'previous',
  6.             todaybutton: 'today',
  7.             nextbutton: 'next',
  8.             resetbutton: 'reset' });
  9.  

And then you get:

timeframe

Check out the live example to really see it at work.

Posted by Dion Almaer at 6:25 am
10 Comments

++++-
4.5 rating from 26 votes

Monday, April 21st, 2008

ProtoRPG: Role Playing with Prototype

Category: JavaScript, Prototype, Games, Scriptaculous

Sometimes you want Monday to be a Friday, so we have ProtoRPG, a role playing game written by Pierre Chassaing in JavaScript using Prototype and Script.aculo.us.

Walk around, add to your inventory, and feel like you are playing your first RPG many moons ago, on a Friday.

Proto RPG

Posted by Dion Almaer at 7:32 am
1 Comment

++++-
4.3 rating from 6 votes

Thursday, April 17th, 2008

Mad Mimi: WYSIWYG Email Marketing

Category: Prototype, Showcase

When I hear "email marketing" I can't help but think spam, but it is a legit tool too, and the latest tool in the pack is Mad Mimi.

Mad Mimi launched this week and consists of "state-of-the-art UI design makes for layouts that are easier to
create -– and easier to read – than emails generated by Constant Contact, Emma and others, who rely on rigid templates and cluttered,
dated layouts."

Mad Mimi's "modules-based" interface allows users to add picture and text fields, drag them around and add captions, links and dividers. Embedded constraints gently guide the layout, keeping the "designer" from getting into trouble, but providing more plasticity than templates.

The result: a fluid, flexible user interface, and clean, fashionable "Mimi-generated" promotions that represent a fresh approach to email marketing – at a subscription price that trumps the competition.

When I saw that Tobie Langel was on the team, I had to check it out. When you give it a spin you will be able to add images, and then use them to build out your email. There are some really nice subtle features such as the undo support.

Mad Mimi

Posted by Dion Almaer at 7:29 am
11 Comments

+++--
3 rating from 11 votes

Tuesday, April 15th, 2008

jQuery and Prototype Benchmarks

Category: JavaScript, Prototype, jQuery, Performance

Piotr Solnica did a couple of posts on jQuery and Prototype benchmarks back in the day, and John-David Dalton just found them.

In part one, he runs tests such as:

JAVASCRIPT:
  1.  
  2. $('td.first').addClass('marked'); // jQuery
  3.  
  4. $$('td.first').each(function(cell){
  5.   cell.addClassName('marked');
  6. });
  7.  
  8. // or
  9.  
  10. $$('td.first').invoke('addClassName', 'marked');
  11.  

and concludes:

Executed tests show that Prototype seems to be faster then jQuery, with the exception of the new insertion method, which performance should be improved. Although I like jQuery syntax more then Prototype, the performance is way more important then saving few lines of code. Of course tests that I made don’t show how these libraries act in a real application, which is my task for the next part(s) of this article. Despite the results I must admit that I’m very excited about jQuery, my general impression is that this library is more mature then Prototype.

In part two, Piotr uses a custom JavaScript-based testing environment instead of running tests using Firebug profiler. This allows the test suite to run in many browsers, and this time concludes:

Prototype was at least 2 times faster then jQuery in 15 cases, and jQuery was faster then Prototype in 8 cases. What library should I choose? In my case I will stick with Prototype, because it offers the same functionality as jQuery does + more and it’s faster. jQuery is probably better for projects where there’s a need for some fancy UI effects and that’s it, but it’s just an assumption, correct me if I’m wrong…

Posted by Dion Almaer at 5:13 am
16 Comments

++++-
4.7 rating from 153 votes

Thursday, April 3rd, 2008

qGallery: Prototype gallery application

Category: JavaScript, Prototype, Showcase

qGallery

Sebastian Brink has developed a nice looking gallery application called qGallery

It is really simple to use you just have to upload your images in full resolution together with a simple xml file and include the script and a simple div into the webpage. Everything else is done automatically. The gallery is creating every used image on the fly with the help of some php scripts in the background.

It is developed on top of Prototype and a bunch of other libraries.

Posted by Dion Almaer at 7:15 am
14 Comments

++++-
4 rating from 42 votes

Monday, March 17th, 2008

ProtoSafe Eases Compatibility for Prototype

Category: Prototype

With so many new components being developed in a variety of different of JavaScript libraries, it's a natural expectation that sooner or later, you're going to want to mix and match these components within your application. Sometimes though, it's not that easy due to architectural conflicts between different libraries. John David Dalton set out to minimize this pain via his new project called ProtoSafe.

I started ProtoSafe in response to a couple of posts on the Prototype Core Mailing list written by developers frustrated with Prototype’s 3rd party compatibility issues. I did some digging and found a post my Mislav Marohnic (here) explaining an easy way to keep Prototype out of the global namespace by simply wrapping everything in a self-executing anonymous function.

I asked John to explain the best use case for Protosafe and how it helps:

Prototype extends native data type prototypes which makes it very convenient for the developer but when interacting with 3rd party code not written for Prototype, these prototype extensions can cause compatibility issues. The most common is the Array for-in loop issue where by doing a for in loop over an array you get its indexed values plus the method names of all the added helper methods.

The benefit that ProtoSafe provides is that it allows Prototype to be used alongside 3rd party code without causing these compatibility issues.
Also it can be run against multiple frameworks which is good in the widget environment.

The demo page shows MooTools,Prototype,Dojo,YUI,jQuery all running on the same page.

Some highlights of ProtoSafe are:

  • It allows Prototype to work with 3rd-party scripts (no Array for-in
    loop pollution).
  • It works with Prototype 1.4-1.6.0.2.
  • Custom Packer3 that supports Prototype 1.6+/Scripty 1.8.1+
  • Only 3kb gzipped, less when compiled into the standalone js.

Posted by Rey Bango at 10:41 am
8 Comments

++++-
4.8 rating from 54 votes

Tuesday, March 11th, 2008

ProtoFlow: Coverflow for Prototype

Category: JavaScript, Component, Prototype, Scriptaculous

Obaid Ahmed has written a coverflow-like component on top of Prototype and Script.aculo.us called ProtoFlow.

It is simple to use:

HTML:
  1.  
  2. <div id="protoflow">
  3.         <img src="imgs/DSCN0940_91360.jpg"/>
  4.         <img src="imgs/stimme_von_oben_187192.jpg"/>
  5.         <img src="imgs/Tropfen_1_Kopie_201721.jpg"/>
  6.         <img src="imgs/farbraum_012_147508.jpg"/>
  7.         <img src="imgs/IMG_4906_199357.jpg"/>
  8.         <img src="imgs/Tropfen_1_Kopie_201721.jpg"/>
  9.         <img src="imgs/Fries_201253.jpg"/>
  10.         <img src="imgs/Fries_201253.jpg"/>
  11. </div>
  12.  
  13. <ul id="protoCaptions" class="protoCaptions">
  14.         <li>Caption 1</li>
  15.         <li>Caption 2</li>
  16.         <li>Caption 3</li>
  17.         <li>Caption 4</li>
  18.         <li>Caption 5</li>
  19.  
  20.         <li>Caption 6</li>
  21.         <li>Caption 7</li>
  22.         <li>Caption 8</li>
  23. </ul>
  24.  
JAVASCRIPT:
  1.  
  2. Event.observe(window, 'load', function() {
  3.   cf = new ProtoFlow($("protoflow"), {captions: 'protoCaptions'});
  4. });
  5.  

ProtoFlow

Posted by Dion Almaer at 7:28 am
15 Comments

+++--
3.8 rating from 38 votes

Tuesday, February 26th, 2008

Canvas Polar Clock

Category: JavaScript, Prototype, Articles, Canvas

"agrath" has developed a very nice Canvas example using Prototype that makes use of a common Prototype-isms: Object.extend, Class.create, bind, enumerables, $w, $.

The Polar Clock is a different visualization of time and the example walks through all of the work required to implement it using Prototype 1.6.0.2 and Canvas, including the draw method:

JAVASCRIPT:
  1.  
  2. draw:function(){
  3.     this.clearCanvas();
  4.     var w = 20;
  5.     var r = 260;
  6.     this.date = new Date();
  7.     var cr = r;
  8.     $('labels').update()
  9.     $w("month day weekday space hour min second").reverse().each(function(interval){
  10.         cr = cr - w - w/2;
  11.         if(interval != 'space')
  12.         {
  13.             var ir = this.intervalToDegrees[interval].bind(this.date)();
  14.             var i = ((ir / 360) * 255) + 147;
  15.             var color = this.cc.rgbToCss(this.cc.hslToRgb(i.wrap(0,255),205,127));
  16.             this.drawSolidArc(color, cr, w, ir.toRadians());           
  17.             $('labels').insert(this.getIntervalLabel[interval].bind(this.date)() + '<br />');
  18.         }
  19.     }.bind(this));
  20. },
  21.  

The article walks through a lot of Canvas as well as the Prototype side of things. Very thorough.

Posted by Dion Almaer at 6:31 am
3 Comments

+++--
3.7 rating from 18 votes

Thursday, February 21st, 2008

Motionbox EventHandler: Event Subscription and Delegation

Category: JavaScript, Library, Prototype

The developers of MotionBox have written a custom Prototype.js based library to handle event delegation and they decided to open source their work.

They told me:

As you know, the technique of listening to events high in the DOM adds a lot of benefits. The two most notable are:

  1. The ability to bind to events before the elements are available in the DOM. This is especially useful for AJAX applications where the DOM is being updated as there is no need to (re)create all your observers. Using this library, the developer doesn't care if an element exists or not, he/she can simply say "any thing with this class works this way" and "anything with this ID does this" All with a simple interface: MBX.EventHandler.subscribe("#myId", "click", myHandlerFunction);
  2. Since we handle events outside of the usual manner, custom event handling becomes trivial. The subscription interface is exactly the same, and custom events can be fired easily with: MBX.EventHandler.fireCustom(domElement, "my_custom_event_name");

Given the following doc:

HTML:
  1.  
  2. function my_listener(evt) {
  3.    console.dir(evt);
  4. }
  5. </script>
  6.   <ul id="unordered_list" class="my_ul_class">
  7.     <li id="one" class="my_li_class">First</li>
  8.     <li id="two" class="my_li_class">Second</li>
  9.   </ul>
  10. </body>
  11.  

You can do the following:

JAVASCRIPT:
  1.  
  2. MBX.EventHandler.subscribe("#one", "click", my_listener)// this will trigger 'my_listener' only by clicking on the li with the id of "one"
  3.  
  4. MBX.EventHandler.subscribe(".my_li_class", "click", my_listener)// this will trigger 'my_listener' by clicking either of the LIs below