From 0ae3ac0c054a3b9ae0b37cc3f52e20f1367c29d2 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sat, 1 Nov 2014 00:38:21 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@30131 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/js/revisions.js | 73 ++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/src/wp-admin/js/revisions.js b/src/wp-admin/js/revisions.js index 4be573eb0a..28949106ae 100644 --- a/src/wp-admin/js/revisions.js +++ b/src/wp-admin/js/revisions.js @@ -49,19 +49,6 @@ window.wp = window.wp || {}; }); }; - // wp_localize_script transforms top-level numbers into strings. Undo that. - if ( revisions.settings.to ) { - revisions.settings.to = parseInt( revisions.settings.to, 10 ); - } - if ( revisions.settings.from ) { - revisions.settings.from = parseInt( revisions.settings.from, 10 ); - } - - // wp_localize_script does not allow for top-level booleans. Fix that. - if ( revisions.settings.compareTwoMode ) { - revisions.settings.compareTwoMode = revisions.settings.compareTwoMode === '1'; - } - /** * ======================================================================== * MODELS @@ -87,13 +74,13 @@ window.wp = window.wp || {}; this.listenTo( this.frame, 'change:compareTwoMode', this.updateMode ); // Listen for internal changes - this.listenTo( this, 'change:from', this.handleLocalChanges ); - this.listenTo( this, 'change:to', this.handleLocalChanges ); - this.listenTo( this, 'change:compareTwoMode', this.updateSliderSettings ); - this.listenTo( this, 'update:revisions', this.updateSliderSettings ); + this.on( 'change:from', this.handleLocalChanges ); + this.on( 'change:to', this.handleLocalChanges ); + this.on( 'change:compareTwoMode', this.updateSliderSettings ); + this.on( 'update:revisions', this.updateSliderSettings ); // Listen for changes to the hovered revision - this.listenTo( this, 'change:hoveredRevision', this.hoverRevision ); + this.on( 'change:hoveredRevision', this.hoverRevision ); this.set({ max: this.revisions.length - 1, @@ -242,6 +229,7 @@ window.wp = window.wp || {}; _.bindAll( this, 'getClosestUnloaded' ); this.loadAll = _.once( this._loadAll ); this.revisions = options.revisions; + this.postId = options.postId; this.requests = {}; }, @@ -339,7 +327,7 @@ window.wp = window.wp || {}; options.context = this; options.data = _.extend( options.data || {}, { action: 'get-revision-diffs', - post_id: revisions.settings.postId + post_id: this.postId }); var deferred = wp.ajax.send( options ), @@ -390,16 +378,19 @@ window.wp = window.wp || {}; }, initialize: function( attributes, options ) { - var properties = {}; - + var state = this.get( 'initialDiffState' ); _.bindAll( this, 'receiveDiff' ); this._debouncedEnsureDiff = _.debounce( this._ensureDiff, 200 ); this.revisions = options.revisions; - this.diffs = new revisions.model.Diffs( [], { revisions: this.revisions }); - // Set the initial diffs collection provided through the settings - this.diffs.set( revisions.settings.diffData ); + this.diffs = new revisions.model.Diffs( [], { + revisions: this.revisions, + postId: this.get( 'postId' ) + } ); + + // Set the initial diffs collection. + this.diffs.set( this.get( 'diffData' ) ); // Set up internal listeners this.listenTo( this, 'change:from', this.changeRevisionHandler ); @@ -409,12 +400,13 @@ window.wp = window.wp || {}; this.listenTo( this.diffs, 'ensure:load', this.updateLoadingStatus ); this.listenTo( this, 'update:diff', this.updateLoadingStatus ); - // Set the initial revisions, baseUrl, and mode as provided through settings - properties.to = this.revisions.get( revisions.settings.to ); - properties.from = this.revisions.get( revisions.settings.from ); - properties.compareTwoMode = revisions.settings.compareTwoMode; - properties.baseUrl = revisions.settings.baseUrl; - this.set( properties ); + // Set the initial revisions, baseUrl, and mode as provided through attributes. + + this.set( { + to : this.revisions.get( state.to ), + from : this.revisions.get( state.from ), + compareTwoMode : this.revisions.get( state.compareTwoMode ) + } ); // Start the router if browser supports History API if ( window.history && window.history.pushState ) { @@ -1143,15 +1135,30 @@ window.wp = window.wp || {}; * Initialize the revisions UI for revision.php. */ revisions.init = function() { + var state; + // Bail if the current page is not revision.php. if ( ! window.adminpage || 'revision-php' !== window.adminpage ) { return; } + state = new revisions.model.FrameState({ + initialDiffState: { + // wp_localize_script doesn't stringifies ints, so cast them. + to: parseInt( revisions.settings.to, 10 ), + from: parseInt( revisions.settings.from, 10 ), + // wp_localize_script does not allow for top-level booleans so do a comparator here. + compareTwoMode: ( revisions.settings.compareTwoMode === '1' ) + }, + diffData: revisions.settings.diffData, + baseUrl: revisions.settings.baseUrl, + postId: parseInt( revisions.settings.postId, 10 ) + }, { + revisions: new revisions.model.Revisions( revisions.settings.revisionData ) + }); + revisions.view.frame = new revisions.view.Frame({ - model: new revisions.model.FrameState({}, { - revisions: new revisions.model.Revisions( revisions.settings.revisionData ) - }) + model: state }).render(); };