Alias Method Chain in JavaScript

Rails introduce a pattern called alias\_method\_chain in 2006. The general idea is you want to add something to an existing method, but you don’t want to monkey patch the whole method.

I found myself wanting this functionality in JavaScript. My use case was adding a callback to the Ajax.Autocompleter in script.aculo.us. Here is how I solved it.

This is the code that defines aliasMethodChain on Object.

  Object.extend(Object, {
    aliasMethodChain: function(object, target,
          feature, implementation) {
      object[target + "Without" +
          feature.charAt(0).toUpperCase() +
          feature.substring(1)] = object[target];
      object[target] = implementation;
    }
  });

And here I am using the aliasMethodChain.

  Object.aliasMethodChain(Autocompleter.Base.prototype,
        "selectEntry", "selectCallback", function() {
    this.selectEntryWithoutSelectCallback();
    this.options.onSelect ? this.options.onSelect() : null;
  });

Nothin’ to it. Now I can pass onSelect into the options of any autocompleter and it will be called when the user makes a selection.

JSON Quickly Quietly Become Defacto Standard?

I have long been a fan of JSON. At RideCharge we still work with xml and soap, and they are fine (still better than FTP drops). The truth is when you have to do B2B work you usually agree to almost anything to get your work done.

But when you release a web service for public consumption, you really need to think about in what format you want to expose your data. We expose most of our APIs in JSON, XML, and YAML. We only get this luxury because it takes almost no effort to do all three. I mean, come on, who would expose something in YAML otherwise J.

That is why it was semi surprising when Google quietly announced their new server-side AJAX API would be JSON only. At first it made very little sense to me, but after some thought, it makes reasonable sense. I declared that 2007 was going to be the year of JavaScript Only APIs (I am a tech blogger not a futurist), because Google released their AJAX Search API as JavaScript only. But now I see it is much more about supporting only one output format, JSON. Whether it is consumed by you, me, Google or Google AJAX JS API. They realize their data is valuable enough, if you want it, you will write:


JSON.parse(response.body)

Git Train - All Aboard

After seeing about a hundred posts on git, I decided to give it a try. It seems to be much better at branching, merging and most importantly submitting patches. And for that reason I decided to dump svn in favor of github for simpltry widgets.

I encourage you to fork my repository and send me a pull request. Or if you don’t want to fork my project, you can fork prototype or rails. What I am really saying is, if you want to contribute to the ruby open source community you are going to have to break down and learn git.

Feeds/Syndication