I am creating a few functions that wrap around jQuery's Ajax function to make it a bit quicker and easier to perform a request. For this I have this JavaScript file:
$( document ).ready( function ()
{
var ajax = {
url: "", type: "", success: "", beforeSend: "",
preform: function ( $url, $type, $success, $beforeSend )
{
$.ajax( {
url: this.url = $url,
type: this.type = $type,
success: setSuccess(this.success = $success),
beforeSend: setBeforeSend(this.beforeSend = $beforeSend)
} );
}
};
function setSuccess( $selector )
{
return function ( $r )
{
$( $selector ).html( $r );
};
}
function setBeforeSend( $selector )
{
return function ()
{
$( $selector ).html( "Loading..." );
};
}
} );
And this is used with the following line of code:
ajax.preform( "script/javascript/ajaxfiles/print.php", "GET", "#success", "#before" );
This is working brilliantly, but is there a better way to do this?