Skip to content

Commit

Permalink
Fix option value not going away during ajax call
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikola Miljkovic committed Feb 19, 2019
1 parent b17ef51 commit 099faee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
35 changes: 22 additions & 13 deletions assets/js/admin/settings/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ function sm_hide_show_elements( target_value, current_value, not, table_row ) {

// If we should get options via Ajax.
if ( is_ajax ) {
sm_reset_option_value( element, ! table_row.hasClass( 'hidden' ) );

let data = {
'action': 'sm_settings_get_select_data',
'id': current_value,
Expand All @@ -134,19 +136,7 @@ function sm_hide_show_elements( target_value, current_value, not, table_row ) {
}
).always(
function () {
// Reset element value.
switch ( element.prop( 'tagName' ) ) {
case 'SELECT':
element.find( 'option' ).each(
function () {
jQuery( this.remove() );
}
);
if ( ! table_row.hasClass( 'hidden' ) ) {
element.append( jQuery( '<option/>' ).val( '' ).text( 'Loading...' ) );
}
break;
}
sm_reset_option_value( element, ! table_row.hasClass( 'hidden' ) );
}
).done(
function ( response ) {
Expand Down Expand Up @@ -199,6 +189,25 @@ function sm_hide_show_elements( target_value, current_value, not, table_row ) {
}
}

/**
* Clears the current option value.
*
* @type {object} The option element.
* @type {boolean} If we should show that it's loading.
*/
function sm_reset_option_value( element, write_loading ) {
switch ( element.prop( 'tagName' ) ) {
case 'SELECT':
element.find( 'option' ).each(
function () {
jQuery( this.remove() );
}
);
}
if ( write_loading ) {
element.append( jQuery( '<option/>' ).val( '' ).text( 'Loading...' ) );
}
}

/**
* Checks to see if a value is set.
Expand Down
2 changes: 1 addition & 1 deletion assets/js/admin/settings/conditionals.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 099faee

Please sign in to comment.