This content has been marked as final. Show 6 replies
If its a local text file it seems unusual to try and load it twice and particularly in quick succession like that. For testing its interesting but in practice you should never need to retrieve the same information from a text file twice if it doesn't change, and if it is being changed (by some external process) then you would probably need to poll it with a reasonable interval between loads.
Perhaps its the fact that the loads are being issued in rapid succession that causes it, with really no time (well not much anyway) between calls.
To test this try changing the order of the two .load commands.
See if it works the other way around. It might be that the last issued load when called in quick succession from the same source takes precedence . Don't know...just guessing could be way off track.
You are right, that it is true that I should never need to reload anything twice like that. I only put in “anotherLoader” to test when I noticed that “loaderObject” was unable to load the file initially. Then it struck me as very odd that “anotherLoader” was able to load it, while “loaderObject” couldn’t.
To test your theory, I did swap the lines and I get the same output (just different order printed). And yes, I should have mentioned this is a local text file in the same folder.
you have a reference problem inside loaderObject.onLoad. "this" will refer to your loaderObject within that methods scope, but loaderObject will not.
Ah okay, I think I can see what is going on here. When I wrote ‘loaderObject’ inside the onload for ‘loaderObject’ , it was trying to find a ‘loaderObject’ within the ‘loaderObject’. So, since I want to get the current ‘loaderObject’, I used ‘this’ to refer to itself inside the onload method. That part works fine now. Thanks for cleaing that part up. =)
But now, I have another problem, I can’t set the contents of what I read in to a variable ‘outside’ of the ‘loaderObject’.
It might make it a bit easier if I post my whole class (below).
‘contentString’ is originally set to “null “. After the init() method runs, I want it to contain the results read in, it should be: “null This is a sample string of text.“ However, onLoad method, can’t even read ‘contentString’, and the resulting string ends up: “undefinedThis is a sample string of text.”
Then of course, when I access ‘contentString’, in another method (such as getText()), I get “null “.
This is the exact output I get when it runs:
this: This is a sample string of text.
inside(after): undefinedThis is a sample string of text.
I think the problem is that inside the onLoad method, it is trying to find a ‘contentString’ inside of ‘loaderObject’. But how can I get ‘loaderObject’ to see ‘contentString’ (the class variable) and modify it?
(adding a reply as it is a different post on a different question)
Ah, I’ve gotten it now. All I did was add a reference to ‘this’ (the class), inside of the loaderObject and accessed the class variable through that reference in the onload method
So, I added this line in the constructor:
loaderObject._outside = this;
(where ‘this’ refers to the class, I believe?)
Then, in the onload method, I was able to access the variable by going:
this._outside.contentString = this.text;
(where ‘this’ refers to the loaderObject)
I guess my question is now, is there a better way to do this? This feels like a quick ‘hack’ to me, or is it actually the ‘correct’ way?
this in the constructor is going to reference a class instance, not the class. i don't think you can reference the class from within the class.