Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Passing additional parameters for your callbacks is possible via some kind of delegate function or closure. However it is often considered a bad practice. You may use event <code>target</code> property instead to determine your index based on <code>FileReference</code>.</p> <p><strong>Edit:</strong> here is a sample of using closures:</p> <pre><code>function getTimerClosure(ind : int) : Function { return function(event : TimerEvent) { trace(ind); }; } for (var i = 0; i &lt; 10; i++) { var tm : Timer = new Timer(100*i+1, 1); tm.addEventListener(TimerEvent.TIMER, getTimerClosure(i)); tm.start(); } </code></pre> <p>This will continuously trace numbers from 0 to 9.</p> <p><strong>Edit2:</strong> here is a sample of creating a delegate based on a function closure:</p> <pre><code>function timerHandler(event : Event, ...rest) : void { trace(event, rest); } function Delegate(scope : Object, func : Function, ...rest) : Function { return function(...args) : void { func.apply(scope, args.concat(rest)); } } var tm : Timer = new Timer(1000, 1); tm.addEventListener(TimerEvent.TIMER, Delegate(this, this.timerHandler, 1, 2, 3)); tm.start(); </code></pre> <p>However this is a bad approach since unsubscribing for such a listener is a hell pain. This in turn will probably cause some memory leakages, which will decrease overall performance of your application. <em>So, use with caution!</em></p> <hr> <p><strong>Bottom line:</strong> if you know how to work with closures, use them - it is a wonderful thing! If you don't care about your application performance in a long perspective, use closures - it's simple!</p> <p>But if you are unsure about closures, use a more conventional approach. E.g. in your case you could create a <code>Dictionary</code> that matches your <code>FileReference</code> objects to appropriate indices. Something like that:</p> <pre><code>var frToInd : Dictionary = new Dictionary(false); // false here wouldn't prevent garbage collection of FileReference objects for (var i : int = 0; i &lt; 10; i++) { // blah-blah stuff with `lib` objects frToInd[lib.fileRef] = i; // another weird stuff and subscription } function eventListener(event : Event) : void { // in the event listener just look up target in the dictionary if (frToInd[event.target]) { var ind : int = frToInd[event.target]; } else { // Shouldn't happen since all FileReferences should be in // the Dictionary. But if this happens - it's an error. } } </code></pre> <p><em>-- Happy coding!</em></p>
 

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