diff options
author | Scott González <scott.gonzalez@gmail.com> | 2015-04-23 15:11:41 -0400 |
---|---|---|
committer | Scott González <scott.gonzalez@gmail.com> | 2015-05-14 20:39:51 -0400 |
commit | ad98cb167350b8d929b71bfeefc904c8964893dd (patch) | |
tree | 47288df5a25877efa1e41d60807e44f3eec68509 /ui/form-reset-mixin.js | |
parent | 556b2710f0f09b76909b92c751edc3f4243fa5c0 (diff) | |
download | jquery-ui-ad98cb167350b8d929b71bfeefc904c8964893dd.tar.gz jquery-ui-ad98cb167350b8d929b71bfeefc904c8964893dd.zip |
Form Reset: Add form reset mixin
Fixes #12638
Closes gh-1555
Diffstat (limited to 'ui/form-reset-mixin.js')
-rw-r--r-- | ui/form-reset-mixin.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/ui/form-reset-mixin.js b/ui/form-reset-mixin.js new file mode 100644 index 000000000..a751f7f2a --- /dev/null +++ b/ui/form-reset-mixin.js @@ -0,0 +1,62 @@ +( function( factory ) { + if ( typeof define === "function" && define.amd ) { + + // AMD. Register as an anonymous module. + define( [ + "jquery", + "ui/core" + ], factory ); + } else { + + // Browser globals + factory( jQuery ); + } +}( function( $ ) { + +return $.ui.formResetMixin = { + _formResetHandler: function() { + var form = $( this ); + + // Wait for the form reset to actually happen before refreshing + setTimeout( function() { + var instances = form.data( "ui-form-reset-instances" ); + $.each( instances, function() { + this.refresh(); + } ); + } ); + }, + + _bindFormResetHandler: function() { + this.form = this.element.form(); + if ( !this.form.length ) { + return; + } + + var instances = this.form.data( "ui-form-reset-instances" ) || []; + if ( !instances.length ) { + + // We don't use _on() here because we use a single event handler per form + this.form.on( "reset.ui-form-reset", this._formResetHandler ); + } + instances.push( this ); + this.form.data( "ui-form-reset-instances", instances ); + }, + + _unbindFormResetHandler: function() { + if ( !this.form.length ) { + return; + } + + var instances = this.form.data( "ui-form-reset-instances" ); + instances.splice( $.inArray( this, instances ), 1 ); + if ( instances.length ) { + this.form.data( "ui-form-reset-instances", instances ); + } else { + this.form + .removeData( "ui-form-reset-instances" ) + .off( "reset.ui-form-reset" ); + } + } +}; + +} ) ); |