From 59456a9cea501fc368e810f68cc5d99215832aad Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Fri, 30 Nov 2012 16:41:38 +0000 Subject: [PATCH] Resolve race conditions in Attachments.more(). Provides for smoother refreshes when searching and properly cleans out content components. Props koopersmith fixes #22656 git-svn-id: http://core.svn.wordpress.org/trunk@22956 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/js/media-models.js | 40 ++++++++++++++++++++++++++-------- wp-includes/js/media-views.js | 2 +- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/wp-includes/js/media-models.js b/wp-includes/js/media-models.js index c3b6dc47d1..385ee5ce01 100644 --- a/wp-includes/js/media-models.js +++ b/wp-includes/js/media-models.js @@ -490,9 +490,27 @@ window.wp = window.wp || {}; }, more: function( options ) { - if ( this.mirroring && this.mirroring.more ) - return this.mirroring.more( options ); - return $.Deferred().resolve().promise(); + var deferred = $.Deferred(), + mirroring = this.mirroring, + attachments = this; + + if ( ! mirroring || ! mirroring.more ) + return deferred.resolveWith( this ).promise(); + + // If we're mirroring another collection, forward `more` to + // the mirrored collection. Account for a race condition by + // checking if we're still mirroring that collection when + // the request resolves. + mirroring.more( options ).done( function() { + if ( this === attachments.mirroring ) + deferred.resolveWith( this ); + }); + + return deferred.promise(); + }, + + hasMore: function() { + return this.mirroring ? this.mirroring.hasMore() : false; }, parse: function( resp, xhr ) { @@ -583,9 +601,9 @@ window.wp = window.wp || {}; options = options || {}; Attachments.prototype.initialize.apply( this, arguments ); - this.args = options.args; - this.hasMore = true; - this.created = new Date(); + this.args = options.args; + this._hasMore = true; + this.created = new Date(); this.filters.order = function( attachment ) { var orderby = this.props.get('orderby'), @@ -627,21 +645,25 @@ window.wp = window.wp || {}; this.observe( wp.Uploader.queue ); }, + hasMore: function() { + return this._hasMore; + }, + more: function( options ) { var query = this; if ( this._more && 'pending' === this._more.state() ) return this._more; - if ( ! this.hasMore ) - return $.Deferred().resolve().promise(); + if ( ! this.hasMore() ) + return $.Deferred().resolveWith( this ).promise(); options = options || {}; options.add = true; return this._more = this.fetch( options ).done( function( resp ) { if ( _.isEmpty( resp ) || -1 === this.args.posts_per_page || resp.length < this.args.posts_per_page ) - query.hasMore = false; + query._hasMore = false; }); }, diff --git a/wp-includes/js/media-views.js b/wp-includes/js/media-views.js index c532e266dc..72b952b002 100644 --- a/wp-includes/js/media-views.js +++ b/wp-includes/js/media-views.js @@ -2821,7 +2821,7 @@ if ( ! this.$el.is(':visible') ) return; - if ( this.el.scrollHeight < this.el.scrollTop + ( this.el.clientHeight * this.options.refreshThreshold ) ) { + if ( this.collection.hasMore() && this.el.scrollHeight < this.el.scrollTop + ( this.el.clientHeight * this.options.refreshThreshold ) ) { this.collection.more().done( this.scroll ); } }