Industry Leader in Digital Business

Howto: Backwards compatibility for Request.JSON in MooTools 1.3

Recently, MooTools released a brand new version of their JavaScript library with new exciting features like the new Slick engine. Though I was at first very excited about this, my excitement was gone within seconds when I noticed that all JavaScript parts of my website did not function properly anymore when using the backward compatible version of MooTools 1.3. After some research, I noticed that calls using Request.JSON were failing on the whole website, attempting to call the ‘onFailure’ method I defined in the options. Realizing this must have been one of the ‘big changes’ that would break backward compatibility, I browsed to the blog with information about the new MooTools 1.3 release. But after reading the whole page I could not find a mention of the issue I had: the issue is not mentioned in the ‘Notable changes in 1.3’-list, neither is it discussed in the blog itself. I realized the new documentation would have to help me out and after some playing with the code I figured it out.It seems that using the post()-method of Request.JSON is no longer possible when setting the data as option. In other words, the following change must be made to your code to make it work again:

Old code (MooTools v1.2):
{code}new Request.JSON({
onFailure: showError,
onSuccess: showArticle,
url: ‘index.php’,
data: {
content: ‘article’,
id: 1
}
}).post();{/code}

New code (MooTools v1.3):
{code}new Request.JSON({
onFailure: showError,
onSuccess: showArticle,
url: ‘index.php’
}).post({
content: ‘article’,
id: 1
});{/code}

This small change fixed all problems I had with dynamically loading data on my website (and it actually looks a bit better ;)). Hopefully it will be of use of you to, so you can also benefit of the new features that MooTools 1.3 has to offer!

Edit 29/10/2010:
I found one other change in behavior of Request.JSON(). In MooTools 1.2 you could request a page (or execute a command on your server) using Request.JSON() and return no data at all. With MooTools 1.3 the method will fail (calling the onFailure-method defined in the options) if you do not return a JSON-object (in my example I returned an empty string with the default server-headers). The fix for this is to use Request() instead since this method does not expect a JSON-object to be returned.

Related Posts