Bind `this` at calltime instead of letting `self` spill down into closures.

See #28510.

Built from https://develop.svn.wordpress.org/trunk@31380


git-svn-id: http://core.svn.wordpress.org/trunk@31361 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2015-02-09 04:45:28 +00:00
parent 308ae4c887
commit ef12a2cc8c
18 changed files with 137 additions and 151 deletions

View File

@ -130,7 +130,7 @@
},
shortcode : function( model ) {
var self = this, content;
var content;
_.each( this.defaults, function( value, key ) {
model[ key ] = self.coerce( model, key );
@ -138,7 +138,7 @@
if ( value === model[ key ] ) {
delete model[ key ];
}
});
}, this );
content = model.content;
delete model.content;
@ -191,15 +191,15 @@
},
shortcode : function( model ) {
var self = this, content;
var content;
_.each( this.defaults, function( value, key ) {
model[ key ] = self.coerce( model, key );
model[ key ] = this.coerce( model, key );
if ( value === model[ key ] ) {
delete model[ key ];
}
});
}, this );
content = model.content;
delete model.content;
@ -1189,14 +1189,12 @@ var PostMedia = Backbone.Model.extend({
},
changeAttachment: function( attachment ) {
var self = this;
this.setSource( attachment );
this.unset( 'src' );
_.each( _.without( wp.media.view.settings.embedExts, this.extension ), function( ext ) {
self.unset( ext );
} );
this.unset( ext );
}, this );
}
});
@ -2831,7 +2829,7 @@ AttachmentsBrowser = View.extend({
controller: this.controller,
priority: -60,
click: function() {
var changed = [], removed = [], self = this,
var changed = [], removed = [],
selection = this.controller.state().get( 'selection' ),
library = this.controller.state().get( 'library' );
@ -2872,10 +2870,10 @@ AttachmentsBrowser = View.extend({
if ( changed.length ) {
selection.remove( removed );
$.when.apply( null, changed ).then( function() {
$.when.apply( null, changed ).then( _.bind( function() {
library._requery( true );
self.controller.trigger( 'selection:action:done' );
} );
this.controller.trigger( 'selection:action:done' );
}, this ) );
} else {
this.controller.trigger( 'selection:action:done' );
}
@ -4145,10 +4143,11 @@ MediaDetails = AttachmentDisplay.extend({
* @returns {media.view.MediaDetails} Returns itself to allow chaining
*/
render: function() {
var self = this;
AttachmentDisplay.prototype.render.apply( this, arguments );
setTimeout( function() { self.resetFocus(); }, 10 );
setTimeout( _.bind( function() {
this.resetFocus();
}, this ), 10 );
this.settings = _.defaults( {
success : this.success

View File

@ -129,7 +129,7 @@
},
shortcode : function( model ) {
var self = this, content;
var content;
_.each( this.defaults, function( value, key ) {
model[ key ] = self.coerce( model, key );
@ -137,7 +137,7 @@
if ( value === model[ key ] ) {
delete model[ key ];
}
});
}, this );
content = model.content;
delete model.content;
@ -190,15 +190,15 @@
},
shortcode : function( model ) {
var self = this, content;
var content;
_.each( this.defaults, function( value, key ) {
model[ key ] = self.coerce( model, key );
model[ key ] = this.coerce( model, key );
if ( value === model[ key ] ) {
delete model[ key ];
}
});
}, this );
content = model.content;
delete model.content;

File diff suppressed because one or more lines are too long

View File

@ -67,18 +67,20 @@ Cropper = State.extend({
requires: { library: false, selection: false },
click: function() {
var self = this,
selection = this.controller.state().get('selection').first();
var controller = this.controller,
selection;
selection.set({cropDetails: this.controller.state().imgSelect.getSelection()});
selection = controller.state().get('selection').first();
selection.set({cropDetails: controller.state().imgSelect.getSelection()});
this.$el.text(l10n.cropping);
this.$el.attr('disabled', true);
this.controller.state().doCrop( selection ).done( function( croppedImage ) {
self.controller.trigger('cropped', croppedImage );
self.controller.close();
controller.state().doCrop( selection ).done( function( croppedImage ) {
controller.trigger('cropped', croppedImage );
controller.close();
}).fail( function() {
self.controller.trigger('content:error:crop');
controller.trigger('content:error:crop');
});
}
}

View File

@ -2663,7 +2663,7 @@ AttachmentsBrowser = View.extend({
controller: this.controller,
priority: -60,
click: function() {
var changed = [], removed = [], self = this,
var changed = [], removed = [],
selection = this.controller.state().get( 'selection' ),
library = this.controller.state().get( 'library' );
@ -2704,10 +2704,10 @@ AttachmentsBrowser = View.extend({
if ( changed.length ) {
selection.remove( removed );
$.when.apply( null, changed ).then( function() {
$.when.apply( null, changed ).then( _.bind( function() {
library._requery( true );
self.controller.trigger( 'selection:action:done' );
} );
this.controller.trigger( 'selection:action:done' );
}, this ) );
} else {
this.controller.trigger( 'selection:action:done' );
}
@ -3198,11 +3198,9 @@ Details = EditImage.extend({
},
save: function() {
var self = this;
this.model.fetch().done( function() {
self.frame.content.mode( 'edit-metadata' );
});
this.model.fetch().done( _.bind( function() {
this.frame.content.mode( 'edit-metadata' );
}, this ) );
}
});
@ -3251,12 +3249,11 @@ EditImage = View.extend({
},
save: function() {
var self = this,
lastState = this.controller.lastState();
var lastState = this.controller.lastState();
this.model.fetch().done( function() {
self.controller.setState( lastState );
});
this.model.fetch().done( _.bind( function() {
this.controller.setState( lastState );
}, this ) );
}
});
@ -3560,8 +3557,6 @@ EditAttachments = MediaFrame.extend({
},
createModal: function() {
var self = this;
// Initialize modal container view.
if ( this.options.modal ) {
this.modal = new Modal({
@ -3569,18 +3564,18 @@ EditAttachments = MediaFrame.extend({
title: this.options.title
});
this.modal.on( 'open', function () {
$( 'body' ).on( 'keydown.media-modal', _.bind( self.keyEvent, self ) );
} );
this.modal.on( 'open', _.bind( function () {
$( 'body' ).on( 'keydown.media-modal', _.bind( this.keyEvent, this ) );
}, this ) );
// Completely destroy the modal DOM element when closing it.
this.modal.on( 'close', function() {
self.modal.remove();
this.modal.on( 'close', _.bind( function() {
this.modal.remove();
$( 'body' ).off( 'keydown.media-modal' ); /* remove the keydown event */
// Restore the original focus item if possible
$( 'li.attachment[data-id="' + self.model.get( 'id' ) +'"]' ).focus();
self.resetRoute();
} );
$( 'li.attachment[data-id="' + this.model.get( 'id' ) +'"]' ).focus();
this.resetRoute();
}, this ) );
// Set this frame as the modal's content.
this.modal.content( this );
@ -4145,10 +4140,11 @@ MediaDetails = AttachmentDisplay.extend({
* @returns {media.view.MediaDetails} Returns itself to allow chaining
*/
render: function() {
var self = this;
AttachmentDisplay.prototype.render.apply( this, arguments );
setTimeout( function() { self.resetFocus(); }, 10 );
setTimeout( _.bind( function() {
this.resetFocus();
}, this ), 10 );
this.settings = _.defaults( {
success : this.success

File diff suppressed because one or more lines are too long

View File

@ -28,14 +28,12 @@ var PostMedia = Backbone.Model.extend({
},
changeAttachment: function( attachment ) {
var self = this;
this.setSource( attachment );
this.unset( 'src' );
_.each( _.without( wp.media.view.settings.embedExts, this.extension ), function( ext ) {
self.unset( ext );
} );
this.unset( ext );
}, this );
}
});

View File

@ -333,18 +333,20 @@ Cropper = State.extend({
requires: { library: false, selection: false },
click: function() {
var self = this,
selection = this.controller.state().get('selection').first();
var controller = this.controller,
selection;
selection.set({cropDetails: this.controller.state().imgSelect.getSelection()});
selection = controller.state().get('selection').first();
selection.set({cropDetails: controller.state().imgSelect.getSelection()});
this.$el.text(l10n.cropping);
this.$el.attr('disabled', true);
this.controller.state().doCrop( selection ).done( function( croppedImage ) {
self.controller.trigger('cropped', croppedImage );
self.controller.close();
controller.state().doCrop( selection ).done( function( croppedImage ) {
controller.trigger('cropped', croppedImage );
controller.close();
}).fail( function() {
self.controller.trigger('content:error:crop');
controller.trigger('content:error:crop');
});
}
}
@ -3836,7 +3838,7 @@ AttachmentsBrowser = View.extend({
controller: this.controller,
priority: -60,
click: function() {
var changed = [], removed = [], self = this,
var changed = [], removed = [],
selection = this.controller.state().get( 'selection' ),
library = this.controller.state().get( 'library' );
@ -3877,10 +3879,10 @@ AttachmentsBrowser = View.extend({
if ( changed.length ) {
selection.remove( removed );
$.when.apply( null, changed ).then( function() {
$.when.apply( null, changed ).then( _.bind( function() {
library._requery( true );
self.controller.trigger( 'selection:action:done' );
} );
this.controller.trigger( 'selection:action:done' );
}, this ) );
} else {
this.controller.trigger( 'selection:action:done' );
}
@ -4395,12 +4397,11 @@ EditImage = View.extend({
},
save: function() {
var self = this,
lastState = this.controller.lastState();
var lastState = this.controller.lastState();
this.model.fetch().done( function() {
self.controller.setState( lastState );
});
this.model.fetch().done( _.bind( function() {
this.controller.setState( lastState );
}, this ) );
}
});
@ -4603,8 +4604,6 @@ EmbedUrl = View.extend({
},
initialize: function() {
var self = this;
this.$input = $('<input id="embed-url-field" type="url" />').val( this.model.get('url') );
this.input = this.$input[0];
@ -4614,9 +4613,9 @@ EmbedUrl = View.extend({
this.listenTo( this.model, 'change:url', this.render );
if ( this.model.get( 'url' ) ) {
_.delay( function () {
self.model.trigger( 'change:url' );
}, 500 );
_.delay( _.bind( function () {
this.model.trigger( 'change:url' );
}, this ), 500 );
}
},
/**
@ -6069,18 +6068,19 @@ ImageDetails = AttachmentDisplay.extend({
},
render: function() {
var self = this,
args = arguments;
var args = arguments;
if ( this.model.attachment && 'pending' === this.model.dfd.state() ) {
this.model.dfd.done( function() {
AttachmentDisplay.prototype.render.apply( self, args );
self.postRender();
} ).fail( function() {
self.model.attachment = false;
AttachmentDisplay.prototype.render.apply( self, args );
self.postRender();
} );
this.model.dfd
.done( _.bind( function() {
AttachmentDisplay.prototype.render.apply( this, args );
this.postRender();
}, this ) )
.fail( _.bind( function() {
this.model.attachment = false;
AttachmentDisplay.prototype.render.apply( this, args );
this.postRender();
}, this ) );
} else {
AttachmentDisplay.prototype.render.apply( this, arguments );
this.postRender();
@ -7793,8 +7793,6 @@ EditorUploader = View.extend({
* Bind drag'n'drop events to callbacks.
*/
initialize: function() {
var self = this;
this.initialized = false;
// Bail if not enabled or UA does not support drag'n'drop or File API.
@ -7814,9 +7812,9 @@ EditorUploader = View.extend({
this.$document.on( 'dragover', _.bind( this.containerDragover, this ) );
this.$document.on( 'dragleave', _.bind( this.containerDragleave, this ) );
this.$document.on( 'dragstart dragend drop', function( event ) {
self.localDrag = event.type === 'dragstart';
});
this.$document.on( 'dragstart dragend drop', _.bind( function( event ) {
this.localDrag = event.type === 'dragstart';
}, this ) );
this.initialized = true;
return this;

File diff suppressed because one or more lines are too long

View File

@ -182,7 +182,7 @@ AttachmentsBrowser = View.extend({
controller: this.controller,
priority: -60,
click: function() {
var changed = [], removed = [], self = this,
var changed = [], removed = [],
selection = this.controller.state().get( 'selection' ),
library = this.controller.state().get( 'library' );
@ -223,10 +223,10 @@ AttachmentsBrowser = View.extend({
if ( changed.length ) {
selection.remove( removed );
$.when.apply( null, changed ).then( function() {
$.when.apply( null, changed ).then( _.bind( function() {
library._requery( true );
self.controller.trigger( 'selection:action:done' );
} );
this.controller.trigger( 'selection:action:done' );
}, this ) );
} else {
this.controller.trigger( 'selection:action:done' );
}

View File

@ -15,11 +15,9 @@ Details = EditImage.extend({
},
save: function() {
var self = this;
this.model.fetch().done( function() {
self.frame.content.mode( 'edit-metadata' );
});
this.model.fetch().done( _.bind( function() {
this.frame.content.mode( 'edit-metadata' );
}, this ) );
}
});

View File

@ -41,12 +41,11 @@ EditImage = View.extend({
},
save: function() {
var self = this,
lastState = this.controller.lastState();
var lastState = this.controller.lastState();
this.model.fetch().done( function() {
self.controller.setState( lastState );
});
this.model.fetch().done( _.bind( function() {
this.controller.setState( lastState );
}, this ) );
}
});

View File

@ -23,8 +23,6 @@ EmbedUrl = View.extend({
},
initialize: function() {
var self = this;
this.$input = $('<input id="embed-url-field" type="url" />').val( this.model.get('url') );
this.input = this.$input[0];
@ -34,9 +32,9 @@ EmbedUrl = View.extend({
this.listenTo( this.model, 'change:url', this.render );
if ( this.model.get( 'url' ) ) {
_.delay( function () {
self.model.trigger( 'change:url' );
}, 500 );
_.delay( _.bind( function () {
this.model.trigger( 'change:url' );
}, this ), 500 );
}
},
/**

View File

@ -74,8 +74,6 @@ EditAttachments = MediaFrame.extend({
},
createModal: function() {
var self = this;
// Initialize modal container view.
if ( this.options.modal ) {
this.modal = new Modal({
@ -83,18 +81,18 @@ EditAttachments = MediaFrame.extend({
title: this.options.title
});
this.modal.on( 'open', function () {
$( 'body' ).on( 'keydown.media-modal', _.bind( self.keyEvent, self ) );
} );
this.modal.on( 'open', _.bind( function () {
$( 'body' ).on( 'keydown.media-modal', _.bind( this.keyEvent, this ) );
}, this ) );
// Completely destroy the modal DOM element when closing it.
this.modal.on( 'close', function() {
self.modal.remove();
this.modal.on( 'close', _.bind( function() {
this.modal.remove();
$( 'body' ).off( 'keydown.media-modal' ); /* remove the keydown event */
// Restore the original focus item if possible
$( 'li.attachment[data-id="' + self.model.get( 'id' ) +'"]' ).focus();
self.resetRoute();
} );
$( 'li.attachment[data-id="' + this.model.get( 'id' ) +'"]' ).focus();
this.resetRoute();
}, this ) );
// Set this frame as the modal's content.
this.modal.content( this );

View File

@ -49,18 +49,19 @@ ImageDetails = AttachmentDisplay.extend({
},
render: function() {
var self = this,
args = arguments;
var args = arguments;
if ( this.model.attachment && 'pending' === this.model.dfd.state() ) {
this.model.dfd.done( function() {
AttachmentDisplay.prototype.render.apply( self, args );
self.postRender();
} ).fail( function() {
self.model.attachment = false;
AttachmentDisplay.prototype.render.apply( self, args );
self.postRender();
} );
this.model.dfd
.done( _.bind( function() {
AttachmentDisplay.prototype.render.apply( this, args );
this.postRender();
}, this ) )
.fail( _.bind( function() {
this.model.attachment = false;
AttachmentDisplay.prototype.render.apply( this, args );
this.postRender();
}, this ) );
} else {
AttachmentDisplay.prototype.render.apply( this, arguments );
this.postRender();

View File

@ -110,10 +110,11 @@ MediaDetails = AttachmentDisplay.extend({
* @returns {media.view.MediaDetails} Returns itself to allow chaining
*/
render: function() {
var self = this;
AttachmentDisplay.prototype.render.apply( this, arguments );
setTimeout( function() { self.resetFocus(); }, 10 );
setTimeout( _.bind( function() {
this.resetFocus();
}, this ), 10 );
this.settings = _.defaults( {
success : this.success

View File

@ -30,8 +30,6 @@ EditorUploader = View.extend({
* Bind drag'n'drop events to callbacks.
*/
initialize: function() {
var self = this;
this.initialized = false;
// Bail if not enabled or UA does not support drag'n'drop or File API.
@ -51,9 +49,9 @@ EditorUploader = View.extend({
this.$document.on( 'dragover', _.bind( this.containerDragover, this ) );
this.$document.on( 'dragleave', _.bind( this.containerDragleave, this ) );
this.$document.on( 'dragstart dragend drop', function( event ) {
self.localDrag = event.type === 'dragstart';
});
this.$document.on( 'dragstart dragend drop', _.bind( function( event ) {
this.localDrag = event.type === 'dragstart';
}, this ) );
this.initialized = true;
return this;

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.2-alpha-31379';
$wp_version = '4.2-alpha-31380';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.