Skip to content

Keep selected options synced with any changes to post settings (from Customize Posts) #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 3, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions js/customize-object-selector-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,26 @@
control.setupAddNewButtons();

api.Control.prototype.ready.call( control );

// Add listener for changes to settings that are in this control.
function watchForChangedSettings( changedSetting ) {
var postId, value = control.setting.get();
var matches = changedSetting.id.match( /^post\[[^\]]+]\[(\d+)]/ );
if ( matches ) {
postId = parseInt( matches[1], 10 );
if ( _.isArray( value ) ? $.inArray( postId, value ) : postId === value ) {
control.populateSelectOptions( true );
}
}
}
api.bind( 'change', watchForChangedSettings );

// Clean up.
api.control.bind( 'remove', function( removedControl ) {
if ( removedControl.id === control.id ) {
wp.customize.unbind( 'change', watchForChangedSettings );
}
} );
},

/**
Expand Down Expand Up @@ -326,14 +346,15 @@
/**
* Re-populate the select options based on the current setting value.
*
* @param {boolean} refresh Whether to force the refreshing of the options.
* @returns {jQuery.promise} Resolves when complete. Rejected when failed.
*/
populateSelectOptions: function() {
populateSelectOptions: function( refresh ) {
var control = this, request, settingValues, selectedValues, deferred = jQuery.Deferred();

settingValues = control.getSettingValues();
selectedValues = control.getSelectedValues();
if ( _.isEqual( selectedValues, settingValues ) ) {
if ( ! refresh && _.isEqual( selectedValues, settingValues ) ) {
deferred.resolve();
} else if ( 0 === settingValues.length ) {
control.select2.empty();
Expand Down