<?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>Barryvan &#187; MooTools</title>
	<atom:link href="http://www.barryvan.com.au/tag/mootools/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.barryvan.com.au</link>
	<description>Music, Programming, Design</description>
	<lastBuildDate>Wed, 23 Jun 2010 04:18:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Limiting the contents of a string via RegEx</title>
		<link>http://www.barryvan.com.au/2009/12/limiting-the-contents-of-a-string-via-regex/</link>
		<comments>http://www.barryvan.com.au/2009/12/limiting-the-contents-of-a-string-via-regex/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 08:10:49 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=332</guid>
		<description><![CDATA[Often, you will need to prevent users from entering data that doesn&#8217;t conform to a specific pattern. For example, you may want to allow users to enter only numbers or only valid email addresses. To this end, I&#8217;ve written a little utility function that returns the &#8220;standardised&#8221; version of a string, according to the regex [...]]]></description>
			<content:encoded><![CDATA[<p>Often, you will need to prevent users from entering data that doesn&#8217;t conform to a specific pattern. For example, you may want to allow users to enter only numbers or only valid email addresses. To this end, I&#8217;ve written a little utility function that returns the &#8220;standardised&#8221; version of a string, according to the regex you supply.</p>
<pre class="brush: javascript">String.implement({
	limitContent: function(allowedRegex) {
		return $splat(this.match(allowedRegex)).join(&#039;&#039;);
	}
});</pre>
<p>Basically, the function takes the result of evaluating the regular expression on the string, converts it into an array if it isn&#8217;t one, and then joins the array&#8217;s elements together with an empty string.</p>
<h2>Examples:</h2>
<pre class="brush: javascript">console.log(&quot;12345&quot;.limitContent(/.{4}/)); // Only allow four characters
console.log(&quot;joe@mail.com&quot;.limitContent(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/)); // Only allow email addresses
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/12/limiting-the-contents-of-a-string-via-regex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MooTools object messaging</title>
		<link>http://www.barryvan.com.au/2009/10/mootools-object-messaging/</link>
		<comments>http://www.barryvan.com.au/2009/10/mootools-object-messaging/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 08:19:08 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=280</guid>
		<description><![CDATA[Events In JavaScript, we often tend to use events all over the place. In MooTools, the custom &#8216;domready&#8217; event is particularly prevalent. However, events suffer from a few drawbacks: You can&#8217;t attach events to non-existent objects. Pretty self-explanatory, really. What this means in practice, though, is that you can&#8217;t easily let object A know when [...]]]></description>
			<content:encoded><![CDATA[<h2>Events</h2>
<p>In JavaScript, we often tend to use events all over the place. In MooTools, the custom &#8216;domready&#8217; event is particularly prevalent. However, events suffer from a few drawbacks:</p>
<h3>You can&#8217;t attach events to non-existent objects.</h3>
<p>Pretty self-explanatory, really. What this means in practice, though, is that you can&#8217;t easily let object A know when object B exists.</p>
<h3>If an object starts to listen for an event after it&#8217;s already fired, it&#8217;ll never hear it.</h3>
<p>Because content on the web isn&#8217;t always delivered in perfect order (especially when you&#8217;re loading scripts synchronously), it&#8217;s possible for an object to try to listen for an event after it&#8217;s already been fired. Obviously, this means that your listener object will never run the code that&#8217;s dependent on that event, which could be Bad Thing™.</p>
<h3>It&#8217;s not particularly easy to know which object is listening for which events.</h3>
<p>There are ways around this, but you can&#8217;t just dir() the listeners in Firebug.</p>
<h2>A messaging system</h2>
<p>For all of these reasons (and probably a few more that I&#8217;ve forgotten about), a messaging system can be an invaluable addition to your arsenal when writing JavaScript. How does a messaging system work? Well, interested objects &#8216;register&#8217; themselves as listeners for particular message &#8216;handles&#8217;, and other objects can send messages using those &#8216;handles&#8217;. Below is a very simple MooTools messaging system that I knocked up, which has a few cool features, including:</p>
<ul>
<li>When you register() a listener, you can have its callback immediately fire if that message has ever been sent before.
<li>
<li>You can very easily see which callbacks are associated with which messages by simply dir()-ing the &#8216;listeners&#8217; member.</li>
<li>You can unregister() a listener at any time (provided you&#8217;ve got a reference to the function and the handle).</li>
<li>Handles can be any valid JavaScript type &#8212; Strings, Numbers, even Objects.</li>
</ul>
<p>Feel free to use and extend this system &#8212; as I mentioned, this is a <strong>very</strong> simple system. If you do extend it, let me know in the comments!</p>
<p>Let me know what you think about this system in the comments.</p>
<pre class="brush: javascript">
barryvan.base.Messaging = new Class({
	listeners: $H(),
	sentMessages: [],

	initialize: function() {

	},

	/**
	 * Register a listener for a particular handle.
	 * handle [String]: The message &#039;handle&#039; to listen for.
	 * callback [Function]: The function to be called when the handle is sent a message. The contents of the messages will be included in the function call.
	 * dontCheck [Boolean]: If falsey and the handle has had a message sent to it, immediately call the callback function (without contents), and continue to add the listener as normal.
	 */
	register: function(handle, callback, dontCheck) {
		if ($type(callback) !== &#039;function&#039;) return;

		if (!dontCheck &amp;&amp; this.sentMessages[handle]) {
			callback();
		}

		if (!this.listeners.has(handle)) this.listeners[handle] = [];
		this.listeners[handle].push(callback);
	},

	/**
	 * Unregister a listener for a particular handle.
	 * handle [String]: The message &#039;handle&#039; to cease listening for.
	 * callback [Function]: The function which was earlier assigned as the callback for the messages.
	 */
	unregister: function(handle, callback) {
		if (this.listeners.has(handle)) {
			this.listeners[handle].erase(callback);
		}
	},

	/**
	 * Send a message to the given handle with the given contents -- send the contents to all the registered listeners for that handle.
	 * handle [String]: The message &#039;handle&#039; to transmit to.
	 * contents [Mixed]: The contents to be sent to the listeners.
	 */
	send: function(handle, contents) {
		this.sentMessages.include(handle);
		if (this.listeners.has(handle)) {
			this.listeners[handle].each(function(callback) {
				callback(contents);
			});
		}
	}
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/10/mootools-object-messaging/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Javascript-generated tables and rowspan</title>
		<link>http://www.barryvan.com.au/2009/04/javascript-generated-tables-and-rowspan/</link>
		<comments>http://www.barryvan.com.au/2009/04/javascript-generated-tables-and-rowspan/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 10:24:43 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=130</guid>
		<description><![CDATA[At work, I&#8217;ve recently been putting together a nice little calendar-like utility using Javascript. Basically, it has to generate a table consisting of cells which may span multiple rows. Surely the solution is simple enough: just set the rowspan on each td as we create it. Unfortunately, that doesn&#8217;t work, at least not in Firefox. [...]]]></description>
			<content:encoded><![CDATA[<p>At work, I&#8217;ve recently been putting together a nice little calendar-like utility using Javascript. Basically, it has to generate a table consisting of cells which may span multiple rows. Surely the solution is simple enough: just set the <em>rowspan</em> on each <em>td</em> as we create it. Unfortunately, that doesn&#8217;t work, at least not in Firefox.</p>
<p>It appears that in Firefox, if you create a <em>td</em> and set its <em>rowspan</em> to some value <strong>when there are no rows for it to expand into</strong>, the attribute will be completely ignored, <strong>even if you add rows afterwards</strong>! Needless to say, this is very annoying. The solution? Build your table backwards.</p>
<p>The code I have now is something like this (note that I&#8217;m developing using the Mootools framework):</p>
<pre class="brush: javascript">var tbl = new Element(&#039;table&#039;);
var trs = new Array();

for (var i = 0; i &lt; 4; i++) {
  var tr = new Element(&#039;tr&#039;);
  tr.grab(new Element(&#039;td&#039;, {
    &#039;html&#039;: &#039;Cell &#039; + i
  }));
  if (i % 2 == 0) {
    tr.grab(new Element(&#039;td&#039;, {
      &#039;rowspan&#039;: 2,
      &#039;html&#039;: &#039;Span &#039; + (i / 2)
    }));
  }
  trs.push(tr);
}

for (var i = trs.length - 1; i &gt;= 0; i--) {
  tbl.grab(trs[i], &#039;top&#039;);
}</pre>
<p>What does this code do? Well basically, we&#8217;re creating a table with ten rows and two columns; the cells in the right-hand column each occupy two rows. The result will be something like this:</p>
<table style="border:solid 1px blue;width:50%;">
<tr>
<td style="border:solid 1px black;">Cell 1</td>
<td rowspan="2" style="border:solid 1px red;">Span 1</td>
</tr>
<tr>
<td style="border:solid 1px black;">Cell 2</td>
</tr>
<tr>
<td style="border:solid 1px black;">Cell 3</td>
<td rowspan="2" style="border:solid 1px red;">Span 2</td>
</tr>
<tr>
<td style="border:solid 1px black;">Cell 4</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/04/javascript-generated-tables-and-rowspan/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MooTools and OO Javascript development</title>
		<link>http://www.barryvan.com.au/2009/04/mootools-and-oo-javascript-development/</link>
		<comments>http://www.barryvan.com.au/2009/04/mootools-and-oo-javascript-development/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 10:26:43 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web design]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=124</guid>
		<description><![CDATA[I&#8217;ve started work on a new project at my job &#8212; a fairly complex AJAX application for the education sector. For this project, I&#8217;ve been allowed to essentially choose my own direction, and I&#8217;ve chosen to implement the clientside Javascript using the MooTools framework. I&#8217;ll say it right here: I&#8217;m absolutely loving it. What I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started work on a new project at my job &#8212; a fairly complex AJAX application for the education sector. For this project, I&#8217;ve been allowed to essentially choose my own direction, and I&#8217;ve chosen to implement the clientside Javascript using the <a title="MooTools Homepage" href="http://mootools.net/">MooTools</a> framework. I&#8217;ll say it right here: I&#8217;m absolutely loving it.</p>
<p>What I&#8217;m really enjoying about <a title="MooTools Homepage" href="http://mootools.net/">MooTools</a> is the object-orientedness it brings to development. Although syntactically it&#8217;s a little bit weird at first, the ability to create, extend, and implement classes makes my development progress much more quickly, and in a more efficient way. Add to that the plethora of utilities (like the <em>.each</em> prototype for arrays) and shorthand functions (like <em>$ </em>to replace <em>document.getElementById</em>), and all of a sudden Javascript development becomes a bit more, well, flexible.</p>
<p>I&#8217;m not saying that you can&#8217;t accomplish cool things in Javascript outside of MooTools (or other frameworks, for that matter); my point is that I believe you can accomplish cool things in Javascript <em>more quickly</em> using a good framework, which should really come as no surprise. Perhaps the reason I&#8217;m so enjoying this type of development, to the point of blogging about it, is that up till now, I&#8217;ve been stuck working in a non-frameworked, very non-OO Javascript development paradigm.</p>
<p>I mentioned the curious syntax that accompanies MooTools.  To create a new class, for example, you would probably write something like this:</p>
<pre class="brush: javascript">var myClass = new Class({
  Implements: Options,
  options: {
    optionA: &#039;monkey&#039;,
    optionB: &#039;pony&#039;
  },
  initializer: function(options) {
    this.setOptions(options);
    this.doSomeStuff();
  },
  doSomeStuff: function() {
    alert(this.options.optionA + &#039; eats &#039; + this.options.optionsB);
  }
});</pre>
<p>And then you would initialise it like this:</p>
<pre class="brush: javascript">var myInstance = new myClass({
  optionA: &#039;Big Pony&#039;
});</pre>
<p>Although it looks a bit weird, it&#8217;s actually not too bad. There are really only two problems I have with it:</p>
<ol>
<li>Remembering to put commas in all the right spots.</li>
<li><a title="The Geany Homepage" href="http://www.geany.org">Geany</a>, my preferred IDE (<a title="Also see my blog entry on a dark colour scheme for Geany using the Tango palette" href="2009/01/geany-ide-tango-dark-colour-scheme/">cf. Geany IDE: Tango dark colour scheme</a>) can&#8217;t pick up classes and members properly (actually, at all) in this style.</li>
</ol>
<p>Other than that, though, I&#8217;m really enjoying it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/04/mootools-and-oo-javascript-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
