Skip to content

Commit

Permalink
Merge pull request #763 from wp-shortcake/758-dont-save-null-values
Browse files Browse the repository at this point in the history
Avoid saving "null" values on empty select2 fields
  • Loading branch information
goldenapples authored Aug 24, 2017
2 parents 3d78174 + c992553 commit e49302b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion js-tests/build/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ Shortcode = Backbone.Model.extend({
this.get( 'attrs' ).each( function( attr ) {

// Skip empty attributes.
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
return;
}

Expand Down
7 changes: 6 additions & 1 deletion js/build/shortcode-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ Shortcode = Backbone.Model.extend({
this.get( 'attrs' ).each( function( attr ) {

// Skip empty attributes.
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
return;
}

Expand Down Expand Up @@ -1754,6 +1754,11 @@ sui.views.editAttributeSelect2Field = sui.views.editAttributeField.extend( {
inputChanged: function(e) {
var _selected = $( e.currentTarget ).val();

// Empty fields will have null values. We don't want to coerce that to the string "null".
if ( _selected === null ) {
_selected = '';
}

// Store multiple selections as comma-delimited list
if ( Array.isArray( _selected ) ) {
_selected = _selected.join( ',' );
Expand Down
2 changes: 1 addition & 1 deletion js/src/models/shortcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Shortcode = Backbone.Model.extend({
this.get( 'attrs' ).each( function( attr ) {

// Skip empty attributes.
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
return;
}

Expand Down
5 changes: 5 additions & 0 deletions js/src/views/select2-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ sui.views.editAttributeSelect2Field = sui.views.editAttributeField.extend( {
inputChanged: function(e) {
var _selected = $( e.currentTarget ).val();

// Empty fields will have null values. We don't want to coerce that to the string "null".
if ( _selected === null ) {
_selected = '';
}

// Store multiple selections as comma-delimited list
if ( Array.isArray( _selected ) ) {
_selected = _selected.join( ',' );
Expand Down

0 comments on commit e49302b

Please sign in to comment.