WordPress.org

Make WordPress Core

Changeset 30131


Ignore:
Timestamp:
11/01/2014 12:38:21 AM (6 years ago)
Author:
wonderboymusic
Message:

Revisions modules should not rely on global settings:

  • Only pass in global settings on init, this allows the classes to be used agnostically elsewhere
  • Clean up some erroneous/weird Backbone syntax

Props ericlewis, wonderboymusic.
Fixes #30219.

File:
1 edited

Legend:

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

    r30130 r30131  
    4949        });
    5050    };
    51 
    52     // wp_localize_script transforms top-level numbers into strings. Undo that.
    53     if ( revisions.settings.to ) {
    54         revisions.settings.to = parseInt( revisions.settings.to, 10 );
    55     }
    56     if ( revisions.settings.from ) {
    57         revisions.settings.from = parseInt( revisions.settings.from, 10 );
    58     }
    59 
    60     // wp_localize_script does not allow for top-level booleans. Fix that.
    61     if ( revisions.settings.compareTwoMode ) {
    62         revisions.settings.compareTwoMode = revisions.settings.compareTwoMode === '1';
    63     }
    6451
    6552    /**
     
    8875
    8976            // Listen for internal changes
    90             this.listenTo( this, 'change:from', this.handleLocalChanges );
    91             this.listenTo( this, 'change:to', this.handleLocalChanges );
    92             this.listenTo( this, 'change:compareTwoMode', this.updateSliderSettings );
    93             this.listenTo( this, 'update:revisions', this.updateSliderSettings );
     77            this.on( 'change:from', this.handleLocalChanges );
     78            this.on( 'change:to', this.handleLocalChanges );
     79            this.on( 'change:compareTwoMode', this.updateSliderSettings );
     80            this.on( 'update:revisions', this.updateSliderSettings );
    9481
    9582            // Listen for changes to the hovered revision
    96             this.listenTo( this, 'change:hoveredRevision', this.hoverRevision );
     83            this.on( 'change:hoveredRevision', this.hoverRevision );
    9784
    9885            this.set({
     
    243230            this.loadAll = _.once( this._loadAll );
    244231            this.revisions = options.revisions;
     232            this.postId = options.postId;
    245233            this.requests  = {};
    246234        },
     
    340328                options.data = _.extend( options.data || {}, {
    341329                    action: 'get-revision-diffs',
    342                     post_id: revisions.settings.postId
     330                    post_id: this.postId
    343331                });
    344332
     
    391379
    392380        initialize: function( attributes, options ) {
    393             var properties = {};
    394 
     381            var state = this.get( 'initialDiffState' );
    395382            _.bindAll( this, 'receiveDiff' );
    396383            this._debouncedEnsureDiff = _.debounce( this._ensureDiff, 200 );
    397384
    398385            this.revisions = options.revisions;
    399             this.diffs = new revisions.model.Diffs( [], { revisions: this.revisions });
    400 
    401             // Set the initial diffs collection provided through the settings
    402             this.diffs.set( revisions.settings.diffData );
     386
     387            this.diffs = new revisions.model.Diffs( [], {
     388                revisions: this.revisions,
     389                postId: this.get( 'postId' )
     390            } );
     391
     392            // Set the initial diffs collection.
     393            this.diffs.set( this.get( 'diffData' ) );
    403394
    404395            // Set up internal listeners
     
    410401            this.listenTo( this, 'update:diff', this.updateLoadingStatus );
    411402
    412             // Set the initial revisions, baseUrl, and mode as provided through settings
    413             properties.to = this.revisions.get( revisions.settings.to );
    414             properties.from = this.revisions.get( revisions.settings.from );
    415             properties.compareTwoMode = revisions.settings.compareTwoMode;
    416             properties.baseUrl = revisions.settings.baseUrl;
    417             this.set( properties );
     403            // Set the initial revisions, baseUrl, and mode as provided through attributes.
     404
     405            this.set( {
     406                to : this.revisions.get( state.to ),
     407                from : this.revisions.get( state.from ),
     408                compareTwoMode : this.revisions.get( state.compareTwoMode )
     409            } );
    418410
    419411            // Start the router if browser supports History API
     
    11441136     */
    11451137    revisions.init = function() {
     1138        var state;
     1139
    11461140        // Bail if the current page is not revision.php.
    11471141        if ( ! window.adminpage || 'revision-php' !== window.adminpage ) {
     
    11491143        }
    11501144
     1145        state = new revisions.model.FrameState({
     1146            initialDiffState: {
     1147                // wp_localize_script doesn't stringifies ints, so cast them.
     1148                to: parseInt( revisions.settings.to, 10 ),
     1149                from: parseInt( revisions.settings.from, 10 ),
     1150                // wp_localize_script does not allow for top-level booleans so do a comparator here.
     1151                compareTwoMode: ( revisions.settings.compareTwoMode === '1' )
     1152            },
     1153            diffData: revisions.settings.diffData,
     1154            baseUrl: revisions.settings.baseUrl,
     1155            postId: parseInt( revisions.settings.postId, 10 )
     1156        }, {
     1157            revisions: new revisions.model.Revisions( revisions.settings.revisionData )
     1158        });
     1159
    11511160        revisions.view.frame = new revisions.view.Frame({
    1152             model: new revisions.model.FrameState({}, {
    1153                 revisions: new revisions.model.Revisions( revisions.settings.revisionData )
    1154             })
     1161            model: state
    11551162        }).render();
    11561163    };
Note: See TracChangeset for help on using the changeset viewer.