Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[t665] onclose event for editors #242

Merged
merged 2 commits into from

3 participants

@secretrobotron
  1. Right before closing, editors will issue a close message through comm.
  2. Rest of "close" code happen (almost) next JS event loop.
@secretrobotron secretrobotron [t665] onclose event for editors
1. Right before closing, editors will issue a close message through comm.
2. Rest of "close" code happen (almost) next JS event loop.
83fad2b
@mjschranz

setTimeout( function(){

@mjschranz

_this.close, false );

@mjschranz

setTimeout( function(){

@mjschranz

_this.close, false );

@jbuck jbuck merged commit 41d3304 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 26, 2012
  1. @secretrobotron

    [t665] onclose event for editors

    secretrobotron authored
    1. Right before closing, editors will issue a close message through comm.
    2. Rest of "close" code happen (almost) next JS event loop.
  2. @secretrobotron

    [t665] Style fixes.

    secretrobotron authored
This page is out of date. Refresh to see the latest.
View
4 editors/default-editor.html
@@ -54,6 +54,10 @@
}
}, false);
+ _comm.listen( "close", function( e ){
+ // use this to process something right before the editor closes
+ });
+
_comm.listen( "trackeventupdated", function( e ){
for( var item in _manifest ){
var element = document.getElementById( item );
View
44 src/dialog/iframe-dialog.js
@@ -39,24 +39,32 @@ define( [
} //onCancel
this.close = function(){
- _parent.removeChild( _iframe );
- if( _modalLayer ){
- _modalLayer.destroy();
- _modalLayer = undefined;
- } //if
- _comm.unlisten( "submit", onSubmit );
- _comm.unlisten( "cancel", onCancel );
- _comm.unlisten( "close", _this.close );
- _comm.destroy();
- _open = false;
- window.removeEventListener( "beforeunload", _this.close, false);
- for( var e in _listeners ){
- if( e !== "close" ){
- _em.unlisten( e, _listeners[ e ] );
- }
- } //for
- _em.dispatch( "close" );
- _em.unlisten( "close", _listeners.close );
+ // Send a close message to the dialog first, then actually close the dialog.
+ // A setTimeout is used here to ensure that its associated function will be run
+ // almost right after the postMessage happens. This ensures that messages get to
+ // their destination before we remove the dom element (which will basically ruin
+ // everything) by placing callbacks in the browser's event loop in the correct order.
+ _this.send( "close" );
+ setTimeout( function(){
+ _parent.removeChild( _iframe );
+ if( _modalLayer ){
+ _modalLayer.destroy();
+ _modalLayer = undefined;
+ } //if
+ _comm.unlisten( "submit", onSubmit );
+ _comm.unlisten( "cancel", onCancel );
+ _comm.unlisten( "close", _this.close );
+ _comm.destroy();
+ _open = false;
+ window.removeEventListener( "beforeunload", _this.close, false );
+ for( var e in _listeners ){
+ if( e !== "close" ){
+ _em.unlisten( e, _listeners[ e ] );
+ }
+ } //for
+ _em.dispatch( "close" );
+ _em.unlisten( "close", _listeners.close );
+ }, 0 );
}; //close
this.open = function( listeners ){
View
48 src/dialog/window-dialog.js
@@ -59,25 +59,33 @@ define( [
} //onError
this.close = function(){
- if( _modalLayer ){
- _modalLayer.destroy();
- _modalLayer = undefined;
- } //if
- _comm.unlisten( "submit", onSubmit );
- _comm.unlisten( "cancel", onCancel );
- _comm.unlisten( "close", _this.close );
- _comm.destroy();
- if( _window.close ){
- _window.close();
- } //if
- clearInterval( _statusInterval );
- window.removeEventListener( "beforeunload", _this.close, false);
- _comm = _window = undefined;
- _open = false;
- for( var e in _listeners ){
- _em.unlisten( e, _listeners[ e ] );
- } //for
- _em.dispatch( "close" );
+ // Send a close message to the dialog first, then actually close the dialog.
+ // A setTimeout is used here to ensure that its associated function will be run
+ // almost right after the postMessage happens. This ensures that messages get to
+ // their destination before we remove the dom element (which will basically ruin
+ // everything) by placing callbacks in the browser's event loop in the correct order.
+ _this.send( "close" );
+ setTimeout( function(){
+ if( _modalLayer ){
+ _modalLayer.destroy();
+ _modalLayer = undefined;
+ } //if
+ _comm.unlisten( "submit", onSubmit );
+ _comm.unlisten( "cancel", onCancel );
+ _comm.unlisten( "close", _this.close );
+ _comm.destroy();
+ if( _window.close ){
+ _window.close();
+ } //if
+ clearInterval( _statusInterval );
+ window.removeEventListener( "beforeunload", _this.close, false );
+ _comm = _window = undefined;
+ _open = false;
+ for( var e in _listeners ){
+ _em.unlisten( e, _listeners[ e ] );
+ } //for
+ _em.dispatch( "close" );
+ }, 0 );
}; //close
function checkWindowStatus(){
@@ -94,7 +102,7 @@ define( [
_listeners[ e ] = listeners[ e ];
} //for
_window = window.open( _url, "dialog-window:" + _url, _features.join( "," ) );
- window.addEventListener( "beforeunload", _this.close, false);
+ window.addEventListener( "beforeunload", _this.close, false );
_comm = new Comm( _window, function(){
_comm.listen( "error", onError );
_comm.listen( "submit", onSubmit );
Something went wrong with that request. Please try again.