mootools 1.11 to 1.2 upgrade path

Fraught with danger. Thankfully, there are some good resources on migrating to the new release. None completely documents all changes, but then, there are a ton of changes in 1.2.

If you were previously sending JSON to your application server, I can no longer find a way of doing that with mootools 1.2. Previously, you’d use Json.Remote for this, which was the following:

Json.Remote = XHR.extend({
 
	initialize: function(url, options){
		this.url = url;
		this.addEvent('onSuccess', this.onComplete);
		this.parent(options);
		this.setHeader('X-Request', 'JSON');
	},
 
	send: function(obj){
		return this.parent(this.url, 'json=' + Json.toString(obj));
	},
 
	onComplete: function(){
		this.fireEvent('onComplete', [Json.evaluate(this.response.text, this.options.secure)]);
	}
 
});

However, the new Request.JSON is merely:

Request.JSON = new Class({
 
	Extends: Request,
 
	options: {
		secure: true
	},
 
	initialize: function(options){
		this.parent(options);
		this.headers.extend({'Accept': 'application/json', 'X-Request': 'JSON'});
	},
 
	success: function(text){
		this.response.json = JSON.decode(text, this.options.secure);
		this.onSuccess(this.response.json, text);
	}
 
});

You’ll immediately notice mootools no longer does any JSON encoding. The new JSON.encode() method is not called here or in Request class itself. What’s more, even if you could send JSON, you cannot override the Content-Type header to application/json unless you first set urlEncoded:false.

The mootools-compat-core.js offers JSON.Remote with the original behavior as an extension of JSON.Request, so clearly someone else missed it too.

Fortunately, in my situation simply reverting to sending application/x-www-form-urlencoded worked okay. For complex data structures, though, it appears there’s an unfortunate regression here.

I realize a link dump is kind of lame, but I haven’t had much time for posting of late.

Post a Comment

Your email is never shared. Required fields are marked *

*
*