Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're right, it has to do with the fact that you're defining a function inside another function, then using it to handle events.</p> <p>Each time the function <code>upload</code> is called, it creates a new <a href="http://en.wikipedia.org/wiki/Closure_(computer_science)" rel="nofollow noreferrer">closure</a>, and assigns a reference to it to the <code>dispatchEvent</code> variable, which is then passed to the <code>addEventListener</code> class. So each time <code>upload</code> is called, it is using a new, <em>different</em> closure in the call to <code>addEventListener</code>. Similarly, in the <code>clearFileUploadListeners</code> function, a new closure is being created on each call (which happens to have the same code each time, but isn't the same function object). The call to <code>removeEventListener</code> does nothing if the given callback has not been added as an event listener for the given event, which is the case here.</p> <p>To solve your problem, you need to store a reference to the closure that you pass to the <code>addEventListener</code> function. This way, you can get a reference to the same closure that was added when you need to remove it later in <code>clearFileUploadListeners</code>.</p> <p>You can try something along the lines of the following code (untested):</p> <pre><code>import flash.utils.Dictionary; var callbackRegistry:* = new Dictionary(); private function upload(file:FileReference, index:String):void { var dispatchEvent:Function = generateFileUploadCompleteCallback(); callbackRegistry[file] = dispatchEvent; file.addEventListener(Event.COMPLETE, dispatchEvent); } private function clearFileUploadListeners(file:FileReference, index:String):void { var dispatchEvent:Function = callbackRegistry[file]; callbackRegistry[file] = null; file.removeEventListener(Event.COMPLETE, dispatchEvent); var bool:Boolean = file.hasEventListener(Event.COMPLETE); if (bool) trace("ERROR"); else trace("YAY, ALL OK!"); } private function generateFileUploadCompleteCallback(index:String):Function { return function(event:Event):void { dispatch(event.type, event, index); }; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload