Skip to content

Commit e49302b

Browse files
authored
Merge pull request #763 from wp-shortcake/758-dont-save-null-values
Avoid saving "null" values on empty select2 fields
2 parents 3d78174 + c992553 commit e49302b

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

js-tests/build/specs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ Shortcode = Backbone.Model.extend({
858858
this.get( 'attrs' ).each( function( attr ) {
859859

860860
// Skip empty attributes.
861-
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
861+
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
862862
return;
863863
}
864864

js/build/shortcode-ui.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ Shortcode = Backbone.Model.extend({
280280
this.get( 'attrs' ).each( function( attr ) {
281281

282282
// Skip empty attributes.
283-
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
283+
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
284284
return;
285285
}
286286

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

1757+
// Empty fields will have null values. We don't want to coerce that to the string "null".
1758+
if ( _selected === null ) {
1759+
_selected = '';
1760+
}
1761+
17571762
// Store multiple selections as comma-delimited list
17581763
if ( Array.isArray( _selected ) ) {
17591764
_selected = _selected.join( ',' );

js/src/models/shortcode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Shortcode = Backbone.Model.extend({
6969
this.get( 'attrs' ).each( function( attr ) {
7070

7171
// Skip empty attributes.
72-
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
72+
if ( ! attr.get( 'value' ) || attr.get( 'value' ).length < 1 ) {
7373
return;
7474
}
7575

js/src/views/select2-field.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ sui.views.editAttributeSelect2Field = sui.views.editAttributeField.extend( {
2020
inputChanged: function(e) {
2121
var _selected = $( e.currentTarget ).val();
2222

23+
// Empty fields will have null values. We don't want to coerce that to the string "null".
24+
if ( _selected === null ) {
25+
_selected = '';
26+
}
27+
2328
// Store multiple selections as comma-delimited list
2429
if ( Array.isArray( _selected ) ) {
2530
_selected = _selected.join( ',' );

0 commit comments

Comments
 (0)