Multiplay Labs

tech hits and tips from Multiplay

Archive for March 9th, 2012

Running named under a jail on FreeBSD

without comments

Seems like this is quite a commonly encountered issue but its actually quite easy to fix, simply add the following to /etc/rc.conf

named_chrootdir=""

If you use rndc on that machine you’ll also want to make sure that controls block also includes your jails primary IP as well e.g.

controls {
    inet 127.0.0.1 allow { 127.0.0.1; <jail-ip>; } keys { "rndc-key"; };
    inet <named-ip> allow { 127.0.0.1; <jail-ip>; } keys { "rndc-key"; };
}

Written by Dilbert

March 9th, 2012 at 4:59 pm

Posted in FreeBSD,OS's

Using the jQuery deferred object pipe method to validate success data

with one comment

Often I’ll make an AJAX call with jQuery to retrieve some data and while that data might be not be erroneous, it might not be exactly what I want. A typical solution to this is to validate the data in the success callback and call out to an external error handler if it fails. However, jQuery provides the capability of setting a failure callback and it would be nice to use that without needing to have to resort to some horrible code like:

var errorHandler = function() {
	alert("ERROR!");
};
 
$.ajax({ 
	url: '/my_ajax_call', 
})
.done(function(data) {
	if(data.success !== 'true') {
		errorHandler();
	} else {
		// do the good stuff
	}
})
.fail(errorHandler);

The problem with the above is that you lose all the benefits of jQuery’s deferred objects[1] and as such if you wanted to attach additional error handlers, you’d have to update the done callback to call them each time which just isn’t feasible when you might be passing around your AJAX promise object all over the place.

However, there is a better way which allows you to leverage the full power of the deferred/promise object system by using the pipe[2] method.

$.ajax({ 
	url: '/my_ajax_call', 
})
.pipe(function(data, textStatus, jqXHR) { 
	if(data.success !== 'true') {
		var deferred = $.Deferred(); 
		deferred.rejectWith( this, [ jqXHR, textStatus, 'invalid' ] ); 
		return deferred; 
	}
	return jqXHR; // return the original deferred object
})
.done(function() { 
	alert('Hurray, a success!');
})
.fail(function() { 
	alert('Boo, a failure!');
})

In the above code, pipe accepts as it’s first argument a function that filters the done callback parameters before the callbacks functions are actually called. Returning null from pipe allows the unfiltered values to pass through, but returning a deferred object will apply the callbacks against that new one, rather than the original. As a result, you can return a deferred object in a failure state to force the failure callbacks to trigger. As you’ll note, using the rejectWith method of the deferred object allows by to fully set what the callbacks evaluation context will be and what arguments it will get. For compatibility, I follow the exact same setup as the defaults, but you’ll see I have set a custom error string.

Because pipe is chainable, you can add multiple of these further down the line. Pipe also accepts additional parameters for filtering other callbacks too. This way it’s much easier and cleaner to validate data and handle errors with the additional flexibility of the deferred object system.

Reference:
[1]: http://api.jquery.com/category/deferred-object/
[2]: http://api.jquery.com/deferred.pipe/

Written by Andrew Montgomery-Hurrell

March 9th, 2012 at 11:18 am

Posted in Hackery,Javascript