Skip to content

Instantly share code, notes, and snippets.

@catrope
Created July 27, 2015 21:31
Show Gist options
  • Save catrope/2593bd431d5cf18b94b9 to your computer and use it in GitHub Desktop.
Save catrope/2593bd431d5cf18b94b9 to your computer and use it in GitHub Desktop.
// BEFORE
mw.flow.ui.EditorSwitcherWidget.prototype.switchEditor = function ( name ) {
var widget = this;
return this.convertContent( ... )
.then( function ( newContent ) {
return newEditor.setup( newContent );
} )
.then( function () {
...
} )
.then( function () {
...
} );
};
// AFTER
mw.flow.ui.EditorSwitcherWidget.prototype.switchEditor = function ( name ) {
if ( this.switchingPromise ) {
return this.switchingPromise;
}
var dummy = $.Deferred(),
widget = this;
this.switchingPromise = dummy
.then( function () {
return widget.convertContent( ... );
} )
.then( function ( newContent ) {
...
} )
.then( function () {
...
} )
.then( function () {
...
} );
// NEW: emit event
this.emit( 'switch', name, this.switchingPromise );
dummy.resolve();
return this.switchingPromise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment