Make WordPress Core

Changeset 51187


Ignore:
Timestamp:
06/20/2021 11:25:55 PM (5 years ago)
Author:
joedolson
Message:

Media: Adapt response shape depending on type of query.

Restore inheriting the backbone fetch in the media library and adapt the AJAX response according to the action performed in the media query.

In [51145], the response shape was restored to the original shape, and a custom fetch was added to handle assigning the totalAttachments information in the collection. The custom fetch triggered a new set of bugs relating to zero-sized collections and loading individual images.

props adamsilverstein, ryelle, peterwilsoncc, Presskopp, desrosj.
Fixes #53421, #53419.

Location:
trunk/src/js
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/wp/util.js

    r51145 r51187  
    116116
    117117                                        if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) {
    118                                                 deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( deferred.jqXHR, [response.data] );
     118
     119                                                // When handling a media attachments request, get the total attachments from response headers.
     120                                                var context = this;
     121                                                deferred.done( function() {
     122                                                        if (
     123                                                                'query-attachments' === action.data.action &&
     124                                                                deferred.jqXHR.hasOwnProperty( 'getResponseHeader' ) &&
     125                                                                deferred.jqXHR.getResponseHeader( 'X-WP-Total' )
     126                                                        ) {
     127                                                                context.totalAttachments = parseInt( deferred.jqXHR.getResponseHeader( 'X-WP-Total' ), 10 );
     128                                                        } else {
     129                                                                context.totalAttachments = 0;
     130                                                        }
     131                                                } );
     132                                                deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
    119133                                        } else {
    120134                                                deferred.rejectWith( this, [response] );
  • trunk/src/js/media/models/attachments.js

    r51145 r51187  
    405405        },
    406406
    407         // Customize fetch so we can extract the total post count from the response headers.
    408         fetch: function(options) {
    409                 var collection = this;
    410                 var fetched = Backbone.Collection.prototype.fetch.call(this, options)
    411                         .done( function() {
    412                                 if ( this.hasOwnProperty( 'getResponseHeader' ) ) {
    413                                         collection.totalAttachments = parseInt( this.getResponseHeader( 'X-WP-Total' ), 10 );
    414                                 } else {
    415                                         collection.totalAttachments = 0;
    416                                 }
    417                         } );
    418                 return fetched;
    419         },
    420 
    421407        /**
    422408         * If the collection is a query, create and mirror an Attachments Query collection.
Note: See TracChangeset for help on using the changeset viewer.