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.

git-svn-id: https://develop.svn.wordpress.org/trunk@51187 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe Dolson 2021-06-20 23:25:55 +00:00
parent c234527301
commit 39ecd7846d
2 changed files with 15 additions and 15 deletions

View File

@ -115,7 +115,21 @@ window.wp = window.wp || {};
}
if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) {
deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( deferred.jqXHR, [response.data] );
// When handling a media attachments request, get the total attachments from response headers.
var context = this;
deferred.done( function() {
if (
'query-attachments' === action.data.action &&
deferred.jqXHR.hasOwnProperty( 'getResponseHeader' ) &&
deferred.jqXHR.getResponseHeader( 'X-WP-Total' )
) {
context.totalAttachments = parseInt( deferred.jqXHR.getResponseHeader( 'X-WP-Total' ), 10 );
} else {
context.totalAttachments = 0;
}
} );
deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
} else {
deferred.rejectWith( this, [response] );
}

View File

@ -404,20 +404,6 @@ var Attachments = Backbone.Collection.extend(/** @lends wp.media.model.Attachmen
});
},
// Customize fetch so we can extract the total post count from the response headers.
fetch: function(options) {
var collection = this;
var fetched = Backbone.Collection.prototype.fetch.call(this, options)
.done( function() {
if ( this.hasOwnProperty( 'getResponseHeader' ) ) {
collection.totalAttachments = parseInt( this.getResponseHeader( 'X-WP-Total' ), 10 );
} else {
collection.totalAttachments = 0;
}
} );
return fetched;
},
/**
* If the collection is a query, create and mirror an Attachments Query collection.
*