0

I have written code as follows.
Problem is that I can't remove Event.COMPLETE event listener and when I call the loadData function twice or more, it works 2 times or more. Sorry for my bad english and worse explanation but I need to fix it today and I don't know what to do.
I think the code is pretty obvious. please help!

var ldr:URLLoader = new URLLoader();

function loadData(text_place, scrollbar, fileURL:String):void {
    text_place.wordWrap = true;
    var f:TextFormat = new TextFormat();
    f.align = TextFormatAlign.RIGHT;
    text_place.setTextFormat(f);
    ldr.dataFormat = URLLoaderDataFormat.TEXT;
    ldr.load(new URLRequest(fileURL));
    ldr.addEventListener(Event.COMPLETE, function ldr_complete(evt:Event){ 
        initText(text_place, ldr.data, scrollbar);
    });
    ldr.addEventListener(IOErrorEvent.IO_ERROR, loadError);
}

function initText(text_place:TLFTextField, fileContent, scrollbar):void {
    ldr.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
    text_place.htmlText = "";
    text_place.tlfMarkup = fileContent;
    scrollbar.update();
    trace("Data loaded");
}

function loadError(e:IOErrorEvent):void {
    trace("Error loading an external file.");
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Farid Rn
  • 3,167
  • 5
  • 39
  • 66
  • 1
    Why don't you just make it a private or public function instead of an anonymous one? – basvk Jan 29 '12 at 16:02
  • 1
    I don't think there's a way to remove it. But you can pass 'true' in addEventListener for useWeakListener. BTW, I think it's better to avoid anon functions until there is no other way – catholicon Jan 29 '12 at 16:10
  • Thanks catholicon. It's seems that useWeakListener solved my problem. Thanks. – Farid Rn Jan 29 '12 at 17:32

3 Answers3

3

just avoid writing function enclosures and extend the scope of the complete function's passed arguments so it can access them.

var ldr:URLLoader = new URLLoader();
var text_place:TextField;
var scrollbar:Object; //or whatever it is

function loadData(text_place, scrollbar, fileURL:String):void
{
    var f:TextFormat = new TextFormat();
    f.align = TextFormatAlign.RIGHT;

    text_place.wordWrap = true;
    text_place.setTextFormat(f);

    scrollbar = scrollbar;

    ldr.dataFormat = URLLoaderDataFormat.TEXT;
    ldr.load(new URLRequest(fileURL));

    ldr.addEventListener(IOErrorEvent.IO_ERROR, loadError);
    ldr.addEventListener(Event.COMPLETE, loadComplete);
}

function initText(text_place:TLFTextField, fileContent, scrollbar):void
{
    removeLoaderEventListeners();

    text_place.htmlText = "";
    text_place.tlfMarkup = fileContent;

    scrollbar.update();

    trace("Data loaded");
}

function loadError(e:IOErrorEvent):void
{
    removeLoaderEventListeners();

    trace("Error loading an external file.");
}

function loadComplete(evt:Event):void
{
    removeLoaderEventListeners();

    initText(text_place, ldr.data, scrollbar);
}

function removeLoaderEventListeners():void
{ 
    ldr.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
    ldr.removeEventListener(Event.COMPLETE, loadComplete);
}
Chunky Chunk
  • 16,553
  • 15
  • 84
  • 162
  • I was using this function like this: `loadData(s6text8, s6sb8, "data/contents/s6text8.xml");`. now how can I use it? – Farid Rn Jan 29 '12 at 17:01
  • and it's also giving me this error: `Implicit coercion of a value of type flash.text:TextField to an unrelated type fl.text:TLFTextField.` on this line: `initText(text_place, ldr.data, scrollbar);` – Farid Rn Jan 29 '12 at 17:09
  • Check your textfield instance on your stage. The TLFTextField is the new type that uses Text Flow. You can change the object to a "Classic" textfield and still use the flash.text:Textfield class. – James Tomasino Jan 30 '12 at 03:39
  • I'm using Persian language and it only works with TLF text. I can't use Classic text! – Farid Rn Jan 30 '12 at 11:15
1

if you want to stop listening for an event after it triggered, you can unregister the anonymous listener in itself:

ldr.addEventListener(Event.COMPLETE, function(event:Event):void
{
     event.target.removeEventListener(event.type, arguments.callee);
     // ... do whatever you need to do here
});

But if you also want to stop listening for other events from the same dispatcher when it completes, such as your IOErrorEvent.IO_ERROR listener, you'd still need a reference to that listener to remove it.

frankhermes
  • 4,720
  • 1
  • 22
  • 39
0

There is a simpler way. Instead of removing event listeners, close the loader.

ldr.close();

Per the documentation:

Closes the load operation in progress. Any load operation in progress is immediately terminated. If no URL is currently being streamed, an invalid stream error is thrown.