1797 | | control.setting.bind( function( value ) { |
| 1797 | /** |
| 1798 | * Set attachment data and render content. |
| 1799 | * |
| 1800 | * Note that BackgroundImage.prototype.ready applies this ready method |
| 1801 | * to itself. Since BackgroundImage is an UploadControl, the value |
| 1802 | * is the attachment URL instead of the attachment ID. In this case |
| 1803 | * we skip fetching the attachment data because we have no ID available, |
| 1804 | * and it is the responsibility of the UploadControl to set the control's |
| 1805 | * attachmentData before calling the renderContent method. |
| 1806 | * |
| 1807 | * @param {number|string} value Attachment |
| 1808 | */ |
| 1809 | function setAttachmentDataAndRenderContent( value ) { |
| 1810 | var hasAttachmentData = $.Deferred(); |
| 1811 | |
| 1812 | if ( control.extended( api.UploadControl ) ) { |
| 1813 | hasAttachmentData.resolve(); |
| 1814 | } else { |
| 1815 | value = parseInt( value, 10 ); |
| 1816 | if ( _.isNaN( value ) || value <= 0 ) { |
| 1817 | delete control.params.attachment; |
| 1818 | hasAttachmentData.resolve(); |
| 1819 | } else if ( control.params.attachment && control.params.attachment.id === value ) { |
| 1820 | hasAttachmentData.resolve(); |
| 1821 | } |
| 1822 | } |
1799 | | // Send attachment information to the preview for possible use in `postMessage` transport. |
1800 | | wp.media.attachment( value ).fetch().done( function() { |
1801 | | wp.customize.previewer.send( control.setting.id + '-attachment-data', this.attributes ); |
| 1824 | // Fetch the attachment data. |
| 1825 | if ( 'pending' === hasAttachmentData.state() ) { |
| 1826 | wp.media.attachment( value ).fetch().done( function() { |
| 1827 | control.params.attachment = this.attributes; |
| 1828 | hasAttachmentData.resolve(); |
| 1829 | |
| 1830 | // Send attachment information to the preview for possible use in `postMessage` transport. |
| 1831 | wp.customize.previewer.send( control.setting.id + '-attachment-data', this.attributes ); |
| 1832 | } ); |
| 1833 | } |
| 1834 | |
| 1835 | hasAttachmentData.done( function() { |
| 1836 | control.renderContent(); |