<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Solutoire.com &#187; Prototype</title>
	<atom:link href="http://solutoire.com/category/prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://solutoire.com</link>
	<description>Publicing platform</description>
	<lastBuildDate>Mon, 21 Feb 2011 18:00:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Monitor Prototype Ajax Requests With Google Analytics</title>
		<link>http://solutoire.com/2007/11/11/monitor-prototype-ajax-requests-with-google-analytics/</link>
		<comments>http://solutoire.com/2007/11/11/monitor-prototype-ajax-requests-with-google-analytics/#comments</comments>
		<pubDate>Sun, 11 Nov 2007 20:47:06 +0000</pubDate>
		<dc:creator>Bas Wenneker</dc:creator>
				<category><![CDATA[Prototype]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[google-analytics]]></category>

		<guid isPermaLink="false">http://solutoire.com/2007/11/11/monitor-prototype-ajax-requests-with-google-analytics/</guid>
		<description><![CDATA[Since Prototype 1.6 final (released this week) all Ajax transport objects are wrapped in an Ajax.Responders object. Using this object we can add generic behavior to Ajax Requests. Imagine you want to show a loading icon every time a request is ongoing. Or even better, monitoring request that are made using Google Analytics. Observing Ajax [...]]]></description>
			<content:encoded><![CDATA[<p>
Since Prototype 1.6 final (released this week) all Ajax transport objects are wrapped in an <code>Ajax.Responders</code> object. Using this object we can add generic behavior to Ajax Requests. Imagine you want to show a loading icon every time a request is ongoing. Or even better, monitoring request that are made using <a href="http://www.google.com/analytics/">Google Analytics</a>.
</p>
<h2>Observing Ajax requests</h2>
<p>
So with the Ajax.Responders object we can observe all kinds of events, fired by <code>Ajax.Request</code>, <code>Ajax.Updater</code> and<code> Ajax.PeriodicalUpdater</code>. These are the events that are observable by <code>Ajax.Responders</code>:
</p>
<ul>
<li>onCreate</li>
<li>onComplete</li>
<li>onException</li>
<li>onInteractive</li>
<li>onLoaded</li>
<li>onLoading</li>
<li>onUninitialized</li>
</ul>
<p>
You can read more about these events on the <a href="http://prototypejs.org/api/ajax/options">Prototype API docs</a>. With the <code>Ajax.Responders</code> object we can register callback functions on these events. In the following example I&#8217;ll show how to monitor Ajax requests using Google Analytics.
</p>
<pre><code class="javascript">Ajax.Responders.register({
	onComplete: function(request){
		// Check if urchinTracker function is available.
		if(typeof(urchinTracker) == 'function' &#038;&#038; request &#038;&#038; request.url){
			// Notice Analytics about the completed request.
			urchinTracker(request.url);
		}
	}
});
</code></pre>
<p>
You can also unregister callback functions. Read more about it at the <a href="http://prototypejs.org/api/ajax/responders">Prototype API docs</a>.</p>
 <img src="http://solutoire.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=121" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://solutoire.com/2007/11/11/monitor-prototype-ajax-requests-with-google-analytics/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Firing Custom Events With The Prototype Javascript Framework</title>
		<link>http://solutoire.com/2007/11/08/firing-custom-events-with-the-prototype-javascript-framework/</link>
		<comments>http://solutoire.com/2007/11/08/firing-custom-events-with-the-prototype-javascript-framework/#comments</comments>
		<pubDate>Thu, 08 Nov 2007 19:15:57 +0000</pubDate>
		<dc:creator>Bas Wenneker</dc:creator>
				<category><![CDATA[Prototype]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://solutoire.com/2007/11/08/firing-custom-events-with-the-prototype-javascript-framework/</guid>
		<description><![CDATA[Yesterday Sam Stephenson released the final version of the Prototype Javascript Framework. I&#8217;ve been using RC0 and RC1 for some time and what I was missing in earlier releases of Prototype&#8217;s the ability to fire custom events. I&#8217;m going to use it to fire events in Plotr. In this post I&#8217;ll explain how to fire [...]]]></description>
			<content:encoded><![CDATA[<p>
Yesterday <a href="http://sam.conio.net/">Sam Stephenson</a> released the final version of the <a href="http://prototypejs.org/">Prototype Javascript Framework</a>. I&#8217;ve been using RC0 and RC1 for some time and what I was missing in earlier releases of Prototype&#8217;s the ability to fire custom events. I&#8217;m going to use it to fire events in <a href="http://www.solutoire.com/plotr/">Plotr</a>. In this post I&#8217;ll explain how to fire custom events, and of course how to observe those events.
</p>
<h2>Observing events</h2>
<p>
Let&#8217;s start by looking at how we can observe events. Imagine we have a form with a button and we want to be noticed when someone clicks it. The <code>id</code> of the button will be &#8216;button&#8217;. The event we&#8217;ll be observing is the <code>click</code> event. In the old days, assigned observer function to DOM elements with the &#8216;onclick&#8217; attribute:
</p>
<pre><code class="html">&lt;input type=&quot;button&quot; onclick=&quot;javascript:buttonClick(event);&quot; id=&quot;button&quot;  value=&quot;Ok&quot;/&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
	function buttonClick(event){
		alert(event);
	}
&lt;/script&gt;
</code></pre>
<blockquote><p>
This shows a very important point: the DOM element you want to observe events from, must exist in the DOM.
</p></blockquote>
<p>
With Prototype, we can do things more unobtrusive:
</p>
<pre><code class="html">&lt;input type=&quot;button&quot; id=&quot;button&quot; value=&quot;Ok&quot;/&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
	// One way to observe the click event on the button
	Element.observe('button', 'click', buttonClick);

	// Another way, but has the same result
	$('button').observe('click', buttonClick);

	function buttonClick(event){
		alert(event);
	}
&lt;/script&gt;</code></pre>
<p>
If you&#8217;re confused by all this, you&#8217;d better read more about the <a href="http://prototypejs.org/api/event">Event Class</a> in the <a href="http://prototypejs.org/api">Prototype API</a>. If you&#8217;re more like, &#8216;hey, let&#8217;s fire some events&#8217;, just read on.
</p>
<h2>Firing custom events</h2>
<p>
Most of the other well known Javascript Frameworks already had the functionality to fire custom events, and it took way too long (imo) before it appeared in Prototype. Anyway, firing events with Prototype is a bit different than other Frameworks. To fire an event, you must (!) add a namespace to the event name, otherwise it won&#8217;t fire. A custom event then would look like this: <code>'namespace:eventname'</code>. This is what <a href="http://prototypejs.org/2007/10/16/prototype-1-6-0-rc1-changes-to-the-class-and-event-apis-hash-rewrite-and-bug-fixes">Sam</a> has to say about it:
</p>
<blockquote><p>
A small but important change was made to custom events in [7835]: all custom event names must now include a namespace. This is our solution to the problem of custom event names conflicting with non-standard native DOM events, such as “mousewheel” and “DOMMouseScroll.”
</p></blockquote>
<p>
In the next example I&#8217;ll show how to fire the event &#8216;bar&#8217; in the &#8216;foo&#8217; namespace (so the event name will be &#8216;foo:bar&#8217;). Passed argument can be accessed by <code>event.memo</code>.
</p>
<pre><code class="javascript">// Function that fires the 'bar' event in the 'foo'
// namespace. Note the second argument of the fire method.
setTimeout(function(){
	$('button').fire('foo:bar', 'foobarbaz');
}, 10000);	

// Let's listen for the 'foo:bar' event.
$('button').observe('foo:bar', function(evt){
	alert(evt.memo); // => foobarbaz
});
</code></pre>
<h2>The Prototype custom &#8216;dom:loaded&#8217; event</h2>
<p>
Prototype 1.6.0 final also comes with the <code>'dom:loaded'</code> event which is fired by the <code>document</code> object. This event&#8217;s fired when the DOM tree is loaded, without waiting for images. This event fires somewhat faster than the window load event, depending on the number of images on the page. The following &#8216;bonus&#8217; example shows the usage of the &#8216;dom:loaded&#8217; event:
</p>
<pre><code class="javascript">// This global variable is set when the dom finishes loading.
var domloaded;

// This custom event is fired when the DOM's finished loading.
// dom:loaded event fires before the window load event.
Element.observe(document, 'dom:loaded', function(){
	domloaded = new Date().getTime();
});

// Observing the plain old window load event.
Element.observe(window, 'load', function(){
	var load = new Date().getTime();
	alert(load - domloaded); // +-35 (ms)
});
</code></pre>
 <img src="http://solutoire.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=120" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://solutoire.com/2007/11/08/firing-custom-events-with-the-prototype-javascript-framework/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Porting Prototype Enumerable functions to Mootools Array objects</title>
		<link>http://solutoire.com/2006/12/25/porting-prototype-enumerable-functions-to-mootools-array-objects/</link>
		<comments>http://solutoire.com/2006/12/25/porting-prototype-enumerable-functions-to-mootools-array-objects/#comments</comments>
		<pubDate>Mon, 25 Dec 2006 13:11:00 +0000</pubDate>
		<dc:creator>Bas Wenneker</dc:creator>
				<category><![CDATA[Prototype]]></category>
		<category><![CDATA[enumerable]]></category>
		<category><![CDATA[Mootools]]></category>

		<guid isPermaLink="false">http://solutoire.com/2006/12/25/porting-prototype-enumerable-functions-to-mootools-array-objects/</guid>
		<description><![CDATA[I really like the Prototype Framework and the functionality that comes with it, but I like Mootools even more because it&#8217;s so damn small and lightweight. Sometimes the minimalistic framework is just too light. Recently I needed some of Prototypes&#8217; Enumerable functions for an Array but the app was implemented using Mootools. So I decided [...]]]></description>
			<content:encoded><![CDATA[<p>
I really like the <a href="http://prototype.conio.net/">Prototype Framework</a> and the functionality that comes with it, but I like <a href="http://www.mootools.net/">Mootools</a> even more because it&#8217;s so damn small and lightweight. Sometimes the minimalistic framework is just too light. Recently I needed some of Prototypes&#8217; Enumerable functions for an Array but the app was implemented using Mootools. So I decided to take a look at the <code>Array.each</code> functions provided by the two frameworks. They&#8217;re implemented in different ways but by changing the Mootools <code>Array.each</code> I was able to treat a Mootools Array like a Prototype Enumerable. In this article I&#8217;ll describe how to &#8216;hack&#8217; the Enumerable functionality into Mootools. If you just want the code you should take a look at my <a href="http://www.solutoire.com/download/EnumArray.js">EnumArray.js</a>.
</p>
<h2>Changing Array.each</h2>
<p>
The <code>Array.each</code> function can be used to loop through all elements of an Array. This functions is used by all Enumerable functions. Here&#8217;s the javascript to let you Mootools&#8217; <code>Array.each</code> work with Enumerable functions:
</p>
<pre><code class="javascript">//needed for adding breaks in Enumerable functions
$break = new Object();
//function that just returns it argument,
//needed in Enumerable functions
Class.ret = function(arg) {return arg};

Array.extend({
	//copy 'native' Mootools Array.each to Array._each
	_each: Array.prototype.each,

	//'overwrite' Array.each to handle a
	//$break thrown by Enumerable functions
	each: function(fn, bind){
		try{
			//try to iterate the Array
			this._each(fn,bind);
		}catch(e){
			//handling a break
			if(e != $break) return e;
		}
	},

	//return the last element of an Array (used in Array.zip())
	last: function() {
   		return this[this.length - 1];
  	}
});</code></pre>
<p>
This code enables us to add Prototype&#8217;s Enumerable functions to an Array. Actually the only thing we&#8217;ve done is enabling our Mootools <code>Array.each</code> to handle a Prototype <code>$break</code> thrown by Enumerable functions.
</p>
<h2>Adding Enumerable functionality</h2>
<p>Let&#8217;s start adding some functions. The first function we&#8217;ll add is Enumerable.all().</p>
<pre><code class="javascript">/**
 * Calls an iterator function to test the values in a
 * list to see if they are all true.
 * @name Array.all()
 * @param {Function} iterator
 *  -Iterator function to call. Arguments elementValue, and elementIndex
 * @return {Boolean} Returns
 *  -true if the iterator returns true for all elements.
 */
Array.extend({
	all: function(iterator) {
	    var result = true;
	    this.each(function(value, index) {
	      result = result &#038;&#038; !!(iterator || Class.ret)(value, index);
	      if (!result) throw $break;
	    });
	    return result;
  	}
});</code></pre>
<p>Ok, great job, but what does it do? Here&#8217;s an example:</p>
<pre><code class="javascript">//our sample array containing three strings
var sample = ['foo','bar','baz'];

//an iterator function that returns false
//when it's argument is equal to 'bar'
var iterator = function(obj, index){
	if(obj == 'bar'){
		return false;
	}
	return true;
};

//iterate the sample array with the iterator function
sample.all(iterator); // => returns false

//iterate the sample array with an anonymous function
//that returns true when it's argument is a string
sample.all(function(obj, index){
	if(typeof obj != 'string'){
		return false;
	}
	return true;
});// => returns true
</code></pre>
<p>
The only thing I changed in the <code>Enumerable.all()</code> code was replacing <code>Prototype.K</code> with <code>Class.ret</code> which are basically the same functions with different names. They bot return their arguments. To see how I added the rest of the Enumerable functions take a look at my <a href="http://www.solutoire.com/download/EnumArray.js">EnumArray.js</a> file. And if you want some examples using these functions you really have to take a look at the <a href="http://encytemedia.com/blog/articles/2005/12/7/prototype-meets-ruby-a-look-at-enumerable-array-and-hash">&#8216;Prototype Meets Ruby: A Look at Enumerable, Array and Hash&#8217;</a> article written by <a href="http://encytemedia.com/">Justin Palmer</a>. Eventually you can take a look at my <a href="http://ajax.solutoire.com">ajax resource site</a> for more Prototype related links.
</p>
<h2>Smaller filesize?</h2>
<p>
The EnumArray.js&#8217;s filesize is quite large (10.5 kb). I&#8217;m pretty sure you don&#8217;t need all the Enumerable functions for your arrays so you could remove the function you don&#8217;t need. Let&#8217;s say we only need the Array.findAll and Array.zip function, then our code will look something like this:
</p>
<pre><code class="javascript">$break = new Object();
Class.ret = function(arg) {return arg};

Array.extend({
	//always include this function
	_each: Array.prototype.each,

	//always include this function
	each: function(fn, bind){
		try{
			this._each(fn,bind);
		}catch(e){
			if(e != $break) return e;
		}
	},

	//needed by Array.zip
	last: function() {
   		return this[this.length - 1];
  	},

	findAll: function(iterator) {
		var results = [];
		this.each(function(value, index) {
	    	if (iterator(value, index))
	        	results.push(value);
	    });
	    return results;
	},

	//zip needs Array.last
	zip: function() {
	    var iterator = Class.ret, args = $A(arguments);
	    if (typeof args.last() == 'function')
			iterator = args.pop();

	    var collections = [this].concat(args).map($A);
	    return this.map(function(value, index) {
	      	iterator(value = collections.pluck(index));
	      	return value;
	    });
	}
});</code></pre>
<p>
This is a lot less code than the full EnumArray.js. For further compression you should use <a href="http://dean.edwards.name/packer/">Dean Edward&#8217;s packer tool</a>.</p>
 <img src="http://solutoire.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=21" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://solutoire.com/2006/12/25/porting-prototype-enumerable-functions-to-mootools-array-objects/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

