Skip to content

Instantly share code, notes, and snippets.

@vaclavbohac
Created June 16, 2011 15:48
Show Gist options
  • Save vaclavbohac/1029545 to your computer and use it in GitHub Desktop.
Save vaclavbohac/1029545 to your computer and use it in GitHub Desktop.
AJAX forms in Nette FW and jQuery mobile
/**
* Formfix enables you to use AJAX forms with Nette FW
* and helps keep tracking of history in jQuery mobile.
*
* Requires jQuery 1.4+, jQuery mobile 1.0
*
* Usage:
* $("#foo").formfix();
*
* @author Vaclav Bohac <bohac.v@gmail.com>
* @copyright Vaclav Bohac (c) 2011
* @license GNU/GPLv3
*/
(function ( $ ) {
var clickedSet = [];
$.fn.formfix = function () {
return this.each(function () {
var submits = $( this ).find( ":submit" );
clickedSet[ this.id ] = null;
submits.click(function ( evt ) {
clickedSet[ $( evt.target ).parent( "form" ).id ] = evt.target;
});
$( this ).submit(function ( evt ) {
var matched, data = {};
$.each( $( evt.target ).serializeArray(), function () {
matched = submits.filter( "[name=" + this.name + "]" );
if ( !matched.length ||
clickedSet[ evt.target.id ] === matched.get( 0 ) ) {
data[ this.name ] = this.value;
}
});
$.mobile.pageLoading();
$.post( evt.target.action, data, function ( resp ) {
if ( Object.hasOwnProperty.call( resp, "redirect" ) ) {
$.mobile.changePage( resp.redirect );
}
});
return false;
});
});
};
}( jQuery ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment