Move the loading of surrounding diffs functionality into the model.

* `wp.revisions.log()` — temporary measure for logging based on `wp.revisions.debug`
* Return promises from functions that sometimes bail but normally return promises.

See #24425.

git-svn-id: http://core.svn.wordpress.org/trunk@24613 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Mark Jaquith 2013-07-09 19:00:11 +00:00
parent 3a94e5706a
commit 245c08fbdf
1 changed files with 57 additions and 26 deletions

View File

@ -8,6 +8,14 @@ window.wp = window.wp || {};
// Link settings. // Link settings.
revisions.settings = typeof _wpRevisionsSettings === 'undefined' ? {} : _wpRevisionsSettings; revisions.settings = typeof _wpRevisionsSettings === 'undefined' ? {} : _wpRevisionsSettings;
// For debugging
revisions.debug = true;
revisions.log = function() {
if ( revisions.debug )
console.log.apply( console, arguments );
}
// wp_localize_script transforms top-level numbers into strings. Undo that. // wp_localize_script transforms top-level numbers into strings. Undo that.
if ( revisions.settings.selectedRevision ) if ( revisions.settings.selectedRevision )
revisions.settings.selectedRevision = parseInt( revisions.settings.selectedRevision, 10 ); revisions.settings.selectedRevision = parseInt( revisions.settings.selectedRevision, 10 );
@ -111,39 +119,48 @@ window.wp = window.wp || {};
}, },
loadNew: function( comparisons ) { loadNew: function( comparisons ) {
comparisons = _.object( comparisons, comparisons ); var self = this;
_.each( comparisons, _.bind( function( id ) { _.each( comparisons, function( id, index ) {
// Exists // Already exists in collection. Don't request it again.
if ( this.get( id ) ) if ( self.get( id ) )
delete comparisons[ id ]; delete comparisons[ index ];
}, this ) ); });
comparisons = _.toArray( comparisons ); wp.revisions.log( 'loadNew', comparisons );
return this.load( comparisons );
if ( comparisons.length )
return this.load( comparisons );
else
return $.Deferred().resolve().promise();
}, },
load: function( comparisons ) { load: function( comparisons ) {
wp.revisions.log( 'load', comparisons );
// Our collection should only ever grow, never shrink, so remove: false // Our collection should only ever grow, never shrink, so remove: false
return this.fetch({ data: { compare: comparisons }, remove: false }); return this.fetch({ data: { compare: comparisons }, remove: false });
}, },
loadLast: function( num ) { loadLast: function( num ) {
num = num || 1; var ids;
var ids = this.getProximalDiffIds();
ids = _.last( ids, num );
if ( ids.length ) { num = num || 1;
ids = _.last( this.getProximalDiffIds(), num );
if ( ids.length )
return this.loadNew( ids ); return this.loadNew( ids );
} else
return $.Deferred().resolve().promise();
}, },
loadLastUnloaded: function( num ) { loadLastUnloaded: function( num ) {
num = num || 1; var ids;
var ids = this.getUnloadedProximalDiffIds();
ids = _.last( ids, num );
if ( ids.length ) { num = num || 1;
ids = _.last( this.getUnloadedProximalDiffIds(), num );
if ( ids.length )
return this.loadNew( ids ); return this.loadNew( ids );
} else
return $.Deferred().resolve().promise();
}, },
getProximalDiffIds: function() { getProximalDiffIds: function() {
@ -235,9 +252,31 @@ window.wp = window.wp || {};
this.listenTo( this, 'change:from', this.changeRevisionHandler ); this.listenTo( this, 'change:from', this.changeRevisionHandler );
this.listenTo( this, 'change:to', this.changeRevisionHandler ); this.listenTo( this, 'change:to', this.changeRevisionHandler );
this.listenTo( this, 'update:revisions', this.loadSurrounding );
this.listenTo( this, 'change:compareTwoMode', this.changedMode );
this.updateDiff({ immediate: true }); this.updateDiff({ immediate: true });
}, },
changedMode: function() {
// This isn't passed from/to so we grab them from the model
this.loadSurrounding( this.get( 'from' ), this.get( 'to' ) );
},
loadSurrounding: function( from, to ) {
// Different strategies for single and compare-two models
if ( this.get( 'compareTwoMode' ) ) {
// TODO: compare-two loading strategy
} else {
// TODO: clean this up to hook in to the ensure process
if ( this.revisions.length ) {
// Load the rest: first 10, then the rest by 50
this.diffs.loadLastUnloaded( 10 ).always( _.bind( function() {
this.diffs.loadAllBy( 50 );
}, this ) );
}
}
},
// Fetch the currently loaded diff. // Fetch the currently loaded diff.
diff: function() { diff: function() {
return this.diffs.get( this._diffId ); return this.diffs.get( this._diffId );
@ -314,14 +353,6 @@ window.wp = window.wp || {};
this.views.set( '.revisions-control-frame', new revisions.view.Controls({ this.views.set( '.revisions-control-frame', new revisions.view.Controls({
model: this.model model: this.model
}) ); }) );
// TODO: The rest of this method should be rewritten and moved into the FrameState.
if ( this.model.revisions.length ) {
// Load the rest: first 10, then the rest by 50
this.model.diffs.loadLastUnloaded( 10 ).always( _.bind( function() {
this.model.diffs.loadAllBy( 50 );
}, this ) );
}
}, },
render: function() { render: function() {