Friday, May 30, 2008

Lightweight JavaScript Events

I've found it handy on a few projects to mix in minimalistic event support to certain JavaScript objects. The basic idea is that you listen (aka "subscribe") for some event on an object, and get notified by the object when it fires (aka "publishes") that event, along with any parameters it decides to attach. This is ridiculously easy to implement in JavaScript, yet it's a really powerful abstraction mechanism for UI programming. It's even easier when you use a library like Prototype to take advantage of its Enumerable and Function extensions. Here it is in under 20 lines of code:
EventManager = {
notify: function() {
if (this._listeners) {
var args = $A(arguments), event = args.shift();
this._listeners.each(function(listener) {
listener[event] && function() {
listener[event].apply(this, args);
}.defer();
}, this);
}
},

listen: function(listener) {
this._listeners = this._listeners || [];
this._listeners.push(listener);
}
};


To add event support to a class, just mixin the EventManager extension:
Foo = Class.create({
setX: function(x) {
this.x = x;
this.notify("setX", this.x);
}
});
Foo.addMethods(EventManager);


And then get down to it:
var object = new Foo();
object.listen({
setX: function(x) {
alert("x was initialized to " + x);
}
});
object.setX(1);


That's about it. Let me know if it comes in handy.

Labels:

Thursday, May 15, 2008

JS Library Roundup

In Part III of the JavaScript Renaissance, we spent another long evening exploring Prototype, Script.aculo.us, and jQuery, and getting a feel for the scope and power of these popular DHTML/Ajax libraries. A good chunk of that time was spent hacking at the Firebug console and experimenting with live HTML documents for immediate feedback.

You can download the presentation here.

Thanks again to the Wheaton ex-Perl Mongers for coming out to learn a little more JavaScript. Come out and see the Perl Mongers reborn as the first arm of the new Polyglot Programmers group!

Addendum: Some of you at the presentation wanted to know to use Greasemonkey to import libraries into web pages after they've already been loaded. Here's the skinny:
  • Go to https://addons.mozilla.org/en-US/firefox/addon/748 and add the Greasemonkey Firefox extension
  • After you restart Firefox you should see the telltale monkey face icon down in the lower right corner of your browser window
  • Right click on the monkey face and select "New User Script..."
  • Give the new script an identifiable name like "load_prototype" or "load_jquery", a namespace (can be anything, I'm not entirely sure what it's even used for), and a list of URLs to include or exclude from running the script
  • Configure a text editor to edit Greasemonkey scripts
  • Finally, enter and save the script

Here's the script I used to load prototype and most of script.aculo.us. Obviously, you'll need to change the paths to point to the files where they're installed on your system. Paste this into your new Greasemonkey document below the comments:

window.addEventListener('load', function() {
var head = document.getElementsByTagName('head')[0];
var libs =
[
'file:///..../prototype.js',
'file:///..../effects.js',
'file:///..../dragdrop.js',
'file:///..../builder.js'
];
for (var i in libs) {
var script = document.createElement('script');
script.type = "text/javascript";
script.src = libs[i];
head.appendChild(script);
}
}, false);


That should be enough to get you started! Please hit me up if you have any questions.

Labels: ,

Sunday, February 17, 2008

Function Arity and JavaScript

A commenter on my last post asked about variable arity methods in JavaScript. Arity, for those unfamiliar with that term, simply refers to the number of parameters a function takes. In dynamic languages, however, arity is a more, well, dynamic concept, since a function can often be called with any number of parameters, or even with a collection to be taken as the argument list. So there are actually two different things going on, which I'll call invocation arity and declaration arity.

There's not much to invocation arity in JavaScript; you've probably seen it hundreds of times before. To determine the arity of invocation from within a method, just use the length property of the array-like arguments object:
SomeType.prototype.foo = function () {
if (arguments.length > 2) {
// do something requiring > 2 arguments
}
}

Declaration arity is similar: I can ask for the length property of the function itself. So for example, if I was passed a function into some higher-order operation, I might check the function's arity to determine how to invoke it:
function foreach(array, callback) {
for (var i = 0; i < array.length; ++i) {
if (callback.length === 1) {
// yield the element
callback(array[i]);
} else {
// yield the index and the element
callback(i, array[i]);
}
}
}

Beware declaration arity though: if your function is trying to be smart about its own arity by foregoing formal parameters and just using its arguments object, it will advertise its arity as zero:
function sum() {
var s = 0;
for (var i = 0; i < arguments.length; ++i) {
s += arguments[i];
}
return s;
}

print(sum.length); // prints 0

Labels:

Thursday, February 14, 2008

More Mongering

The Perl Mongers were at it again on Tuesday night, going above and beyond the call of duty, slogging through snow and slush to listen to more JavaScript tidbits. This time, we explored how to use JavaScript to interact with the browser window and HTML document, monitor and handle user events, and make remote HTTP calls. We finished up with some lively discussion questions about JavaScript libraries and frameworks and how they compete, relate, and stack on top of one another; so I'll try to go more into that question in the near future.

But for now, you can find the slides and samples of the presentation here.

Labels: ,

Friday, February 1, 2008

Currying in Prototype

If you've spent much time in the functional or higher-order programming mindset, you've already gotten a feel for the expressive power that comes from the ability to compose new functions out of existing ones. In pure functional languages, one of the basic types of function composition operations is called "currying" (after Haskell Curry, an early pioneer in this particular space). When you curry a function, you essentially bind parameter values to it, and return a new function over the remaining parameters. Some languages have built-in support for currying as a fundamental operation (ML and Haskell, for example), but you can implement them in any language that supports closures and functions as first-class objects. The Prototype library, as of version 1.6, does this for JavaScript via the Function.prototype.curry method.

Here it is, in all its beautiful compactness (without the Object.extend confusion):
Function.prototype.curry = function() {
if (!arguments.length) return this;
var __method = this, args = $A(arguments);
return function() {
return __method.apply(this, args.concat($A(arguments)));
}
};
We'll walk through line by line:
if (!arguments.length) return this;
If no arguments were bound, simply return the function as-is.
var __method = this, args = $A(arguments);
Store the original function in a closure as __method, and the arguments to be bound as args.
return function() { ... }
Nothing surprising here. We're going to return a new function.
return __method.apply(this, args.concat($A(arguments)));
This is where the magic happens. The newly created function, when called, is going to return the result of calling the original function -- __method.apply(this, ...) -- with the original arguments -- args -- prepended to the invocation arguments.

An illustration is worth a thousand words:
function original(x, y, z) {
return x+y+z;
}

var bound_x = original.curry(10);
bound_x(20, 30) // ==> original(10, 20, 30)
// ==> 60

// sure, you can curry a curried function
var bound_x_y = bound_x.curry(20);
bound_x_y(30) // ==> bound_x(20, 30)
// ==> original(10, 20, 30)
// ==> 60

var bound_x = original.bind(this, 10);
var bound_x_y = bound_x.bind(this, 20);

Labels:

Friday, January 11, 2008

Testing Ajax calls in Prototype

If you're doing any kind of unit testing of Ajax behavior, you'll most likely come upon one minor annoyance. If you test your UI expectations in the context that fires the request, the callback won't have completed yet, and the elements you're setting or creating won't be set up. On the other hand, if you test your expectation in the context that receives the response, the test runner thread will have ended long ago, and any failures won't be caught and reported.

What you really want to do is disable asynchronous callback behavior for your test runs, but unfortunately there is no global configuration parameter for Prototype that controls that.

A little metaprogramming to the rescue.

Simply add the following to the top of your JSSpec test runner to override asynchronous behavior for your tests:

(function () {
var $$setOptions = Ajax.Base.prototype.setOptions;
Ajax.Base.prototype.setOptions = function (options) {
$$setOptions.call(this, options);
this.options.asynchronous = false;
};
})();


(s/setOptions/initialize/g if you're using the latest and greatest Prototype 1.6.)

Just fire the events that cause Ajax requests and test your expectations afterward.

One more thing:

If your Ajax response contains <script> elements, Prototype will kindly extract and execute them for you, provided that you pass evalScripts: true as one of your Ajax options. However, what you may not expect is that it executes them in a deferred callback using the browser's setTimeout call. I initially set out to patch Element.Methods.update, which handles the deferred evaluation behavior, in much the same way as I did setOptions, when it occurred to me that a more general solution would be more helpful for testing. Why not just patch setTimeout and setInterval so that they behave synchronously?

window.setTimeout = window.setInterval = function (f, timeout) {
typeof f === 'string'? eval(f): f();
}


(Again, do this in your test harness, not in your production code!)

Obviously, you're going to completely lose your desired timing behaviors, but if you're trying to test elaborate timing schemes in JSSpec or JSUnit, you're pretty much screwed anyway. But let me know if you find any neat tricks to accomplish that.

Labels: , ,

Wednesday, January 9, 2008

Advanced JavaScript: Not for the Faint of Heart

Thanks to the kind folks from Chicago Perl Mongers, who took two hours out of their busy lives last night to learn a little bit about the dark corners and esoterica of JavaScript.

Those who have only been exposed to procedural and class-based languages often find JavaScript's unique brand of functional programming rather unusual, if not downright confusing. Its lexical scoping and prototype chaining rules allow for some very powerful metaprogramming facilities; but the language is finicky, and the very features that provide so much power can also be badly abused and hacked. It seems fair to say that this has been the rule, rather than the exception, until the dawn of the Ajax revolution in 2005. Did you know that you can easily create arbitrarily deep inheritance hierarchies in JavaScript, even though the language has no concept of classes? Create higher-order functions that can bind or curry parameters to existing functions? Extend language and DOM data types with custom functionality?

The future seems a lot brighter now, thanks to the tireless work of folks like Douglas Crockford, Dean Edwards, and John Resig, and to cross-browser JavaScript libraries like Prototype, jQuery, and Base.

The JavaScript Renaissance, Part I: The Core Language
Presentation
Snippets

Labels: ,

Friday, December 21, 2007

JavaScript 1.7 steals shamelessly from Python

A colleague shared a Planet GNOME article with me this morning about using the Thread.js library to fake threads and write Erlang-style concurrency in JavaScript. After briefly investigating said library, I must reluctantly admit that I didn't realize how much really cool stuff there is in JavaScript 1.7 -- much of which has been imported from Python: generators, iterators, and array comprehensions.

I keep hearing people complaining that JavaScript is becoming too much like Python, and I never understood what they were talking about. Now I do. I just don't understand what they're complaining about.

Mozilla Developer Center - New in JavaScript 1.7

Labels: ,

Thursday, October 25, 2007

Google Books and Google Feeds

I've really been enjoying playing around with Google Books lately. I've wanted to get my (ever-growing) home library into some kind of shared system for years, but the idea of entering hundreds of ISBN's into a spreadsheet just never seemed like how I wanted to spend my time. And even then, how to convert or import that data into something useful?

The first big win was that, with Google Books, I could perform advanced queries, such as by title and author, much the same way I'd perform an advanced web search. So, for example:
intitle:"agile web development with rails" inauthor:"hansson"

Even though the search would be slightly fuzzier than an exact ISBN search, if it meant not having to pull each and every book off the shelf to get the ISBN, I was willing to deal with a few errors and mismatches that I could correct by hand.

Since I was really itching to see my books show up in the library as soon as possible, I decided to go quick and dirty and write a Python screen-scraper. The basic idea is: for each book in the spreadsheet, submit the search query, then scrape the results looking for an "Add to my library" link. However, in order to do that, I first needed to log in to Google Books, and capture my User-Agent and Cookie headers that associated me to my session and my library. That logic comprises one of the only two interesting parts of the bot:
import urllib, urllib2

def getBookSearchRequest(title, author):
query = urllib.quote(
'intitle:"'+title+'" inauthor:"'+author+'"')
req = urllib2.Request(
'http://books.google.com/books'+
'?as_brr=0'+ #the advanced search flag
'&q='+query+
'&btnG=Search+Books')
req.add_header('Host', 'books.google.com')
req.add_header('User-Agent', HARDCODED_USER_AGENT)
req.add_header('Cookie', HARDCODED_GOOGLE_COOKIE)
return req

The other interesting part was scraping for the link:
import re, cgi

for book in books:
page = urllib2.urlopen(
getBookSearchRequest(
book.name, book.author)).read()
match = re.search(
r'<a href="([^"]*)">\s*Add to my library\s*</a>',
page,
re.DOTALL)
if not match:
continue
link = match.group(1)
# handle the result...

The rest basically boils down to HTTP retry logic and gracefully bailing out when no results are found. Anyway, before I had time to groan and say, "Ugh!" I had about 230 out of 250 books imported, most of which were actually not in the system as requested. Not too shabby.

Having a library at Google Books also gave me an opportunity to play with the Google Feed JavaScript API. With a simple JavaScript call, you can retrieve data from any RSS or Atom feed, and dynamically inject it into your page with your Ajax library of choice.

Here is my otherwise empty personal page with the five books most recently added to my library. And here's the JavaScript code to pull the feed:
var feed = new google.feeds.Feed(
'http://books.google.com/books?as_list='+
'BDToX1-EQuq7cjr6nqdzfARoU-HJfh-GeA1cvLGf59B-j5Y0JG3Y'+
'&output=rss');
feed.setNumEntries(5);
feed.load(function(result) {
if (!result.error) {
for (var i = 0; i < result.feed.entries.length; ++i) {
// handle the item...
}
}
});

Labels: , , , ,