Make WordPress Core

Changeset 24613


Ignore:
Timestamp:
07/09/2013 07:00:11 PM (13 years ago)
Author:
markjaquith
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/js/revisions.js

    r24611 r24613  
    88        // Link settings.
    99        revisions.settings = typeof _wpRevisionsSettings === 'undefined' ? {} : _wpRevisionsSettings;
     10
     11        // For debugging
     12        revisions.debug = true;
     13
     14        revisions.log = function() {
     15                if ( revisions.debug )
     16                        console.log.apply( console, arguments );
     17        }
    1018
    1119        // wp_localize_script transforms top-level numbers into strings. Undo that.
     
    112120
    113121                loadNew: function( comparisons ) {
    114                         comparisons = _.object( comparisons, comparisons );
    115                         _.each( comparisons, _.bind( function( id ) {
    116                                 // Exists
    117                                 if ( this.get( id ) )
    118                                         delete comparisons[ id ];
    119                         }, this ) );
    120                         comparisons = _.toArray( comparisons );
    121                         return this.load( comparisons );
     122                        var self = this;
     123                        _.each( comparisons, function( id, index ) {
     124                                // Already exists in collection. Don't request it again.
     125                                if ( self.get( id ) )
     126                                        delete comparisons[ index ];
     127                        });
     128                        wp.revisions.log( 'loadNew', comparisons );
     129
     130                        if ( comparisons.length )
     131                                return this.load( comparisons );
     132                        else
     133                                return $.Deferred().resolve().promise();
    122134                },
    123135
    124136                load: function( comparisons ) {
     137                        wp.revisions.log( 'load', comparisons );
    125138                        // Our collection should only ever grow, never shrink, so remove: false
    126139                        return this.fetch({ data: { compare: comparisons }, remove: false });
     
    128141
    129142                loadLast: function( num ) {
    130                         num     = num || 1;
    131                         var ids = this.getProximalDiffIds();
    132                         ids     = _.last( ids, num );
    133 
    134                         if ( ids.length ) {
     143                        var ids;
     144
     145                        num = num || 1;
     146                        ids = _.last( this.getProximalDiffIds(), num );
     147
     148                        if ( ids.length )
    135149                                return this.loadNew( ids );
    136                         }
     150                        else
     151                                return $.Deferred().resolve().promise();
    137152                },
    138153
    139154                loadLastUnloaded: function( num ) {
    140                         num     = num || 1;
    141                         var ids = this.getUnloadedProximalDiffIds();
    142                         ids     = _.last( ids, num );
    143 
    144                         if ( ids.length ) {
     155                        var ids;
     156
     157                        num = num || 1;
     158                        ids = _.last( this.getUnloadedProximalDiffIds(), num );
     159
     160                        if ( ids.length )
    145161                                return this.loadNew( ids );
    146                         }
     162                        else
     163                                return $.Deferred().resolve().promise();
    147164                },
    148165
     
    236253                        this.listenTo( this, 'change:from', this.changeRevisionHandler );
    237254                        this.listenTo( this, 'change:to', this.changeRevisionHandler );
     255                        this.listenTo( this, 'update:revisions', this.loadSurrounding );
     256                        this.listenTo( this, 'change:compareTwoMode', this.changedMode );
    238257                        this.updateDiff({ immediate: true });
     258                },
     259
     260                changedMode: function() {
     261                        // This isn't passed from/to so we grab them from the model
     262                        this.loadSurrounding( this.get( 'from' ), this.get( 'to' ) );
     263                },
     264
     265                loadSurrounding: function( from, to ) {
     266                        // Different strategies for single and compare-two models
     267                        if ( this.get( 'compareTwoMode' ) ) {
     268                                // TODO: compare-two loading strategy
     269                        } else {
     270                                // TODO: clean this up to hook in to the ensure process
     271                                if ( this.revisions.length ) {
     272                                        // Load the rest: first 10, then the rest by 50
     273                                        this.diffs.loadLastUnloaded( 10 ).always( _.bind( function() {
     274                                                this.diffs.loadAllBy( 50 );
     275                                        }, this ) );
     276                                }
     277                        }
    239278                },
    240279
     
    315354                                model: this.model
    316355                        }) );
    317 
    318                         // TODO: The rest of this method should be rewritten and moved into the FrameState.
    319                         if ( this.model.revisions.length ) {
    320                                 // Load the rest: first 10, then the rest by 50
    321                                 this.model.diffs.loadLastUnloaded( 10 ).always( _.bind( function() {
    322                                         this.model.diffs.loadAllBy( 50 );
    323                                 }, this ) );
    324                         }
    325356                },
    326357
Note: See TracChangeset for help on using the changeset viewer.