11-02-2009, 07:43 PM
|
#1
|
|
Senior Member
Join Date: Dec 2006
Posts: 209
|
AS3 oddity?
I have this code to flexibly create an instance
Code:
var tMediatorClassName:String=tProxy.getMediatorName(note.getBody().id);
var tMediatorClass:Class=getDefinitionByName(tMediatorClassName) as Class;
switch (tMediatorClass.NAME) {
case tMediatorClass.NAME :
facade.registerMediator(new tMediatorClass(note.getBody().id,note.getBody()));
break;
case EventMediator.NAME :
facade.registerMediator(new tMediatorClass(note.getBody().id,note.getBody()));
break;
}
Now the above code works, weirdly enough if you have a look at the above code, you might say, hey why dont you simply go:
Code:
var tMediatorClassName:String=tProxy.getMediatorName(note.getBody().id);
var tMediatorClass:Class=getDefinitionByName(tMediatorClassName) as Class;
facade.registerMediator(new tMediatorClass(note.getBody().id,note.getBody()));
But funnily enough, then the code doesnt work, it thorws an error that the variable 'classname' (in this case 'EventMediator') is not defined.
Even weirder is, that EventMediator is not the only class I am flexibly creating here. BasicMediator is created as well without any problem. It actually only uses the first case for all classes. So seemingly, it doesnt need the EventMediator case. But as soon as I take it away, it misses it. I can replace the EventMediator case with a BasicMediator case and the code works, even though logically there is no necessity for any of them.
But as soon as I take all xxMediator cases away then the code throws an error. Anyone any idea why that would be?
|
|
|
11-02-2009, 08:15 PM
|
#2
|
|
Holosuit User
Join Date: Oct 2006
Location: Tel Aviv
Posts: 3,833
|
Don't cast tMediatorClass to class... or, better, cast it to class when you create new item. This is because Class class doesn't have a NAME property. It may look OK to you, because you expect it to be a static constant, but, in fact, what happens is:
you have object:
ActionScript Code:
var foo:Class =
{
prototype: ...,
hasOwnProperty: ...,
isPropertyEnumerable: ...,
toString: ...
}
And you want to access a NAME property on it, which existence is not known to the compiler at that point.
EDIT:
Oh, silly me. The problem is absolutely different 
You should import and use EventMediator at least once for this class to make it into your SWF, else you'll get the error you're getting, i.e. do something like this:
ActionScript Code:
import EventMediator;EventMediator
Last edited by wvxvw; 11-02-2009 at 08:20 PM.
|
|
|
11-02-2009, 09:15 PM
|
#3
|
|
Senior Member
Join Date: Dec 2006
Posts: 209
|
I am importing the classes, both EventMediator and BasicMediator. I also tried to write out the path properly without asterix, even though that shoulndt make a difference.
It doesnt say that the class is not there it says : ReferenceError: Error #1065: Variable BasicMediator is not defined.
|
|
|
11-02-2009, 09:24 PM
|
#4
|
|
Holosuit User
Join Date: Oct 2006
Location: Tel Aviv
Posts: 3,833
|
This error means exactly that it doesn't know what BasicMediator is, this may only happen when it is neither imported, nor created anywhere in the class scope / function scope.
|
|
|
11-02-2009, 09:40 PM
|
#5
|
|
French Error ...
Join Date: Dec 2007
Location: Vermont, USA
Posts: 5,055
|
I believe you use NAME to differentiate between classes and after all why not. (tell me if I'm wrong). However this is where Interfaces can be of great help. Each of your class can implement an Interface. You can then use many different methods to check if the current class is of type of this or that Interface. For example you could have:
ActionScript Code:
var tMediatorClass:Class = getDefinitionByName(tMediatorClassName) as Class;
try{
var Iobject:IEventMediator = new tMediatorClass(note.getBody().id,note.getBody())
facade.registerMediator(Iobject);
}catch(e:Error){
//didn't work do something
}//and run other try catch for other interfaces...
Last edited by ASWC; 11-02-2009 at 09:53 PM.
|
|
|
11-02-2009, 10:17 PM
|
#6
|
|
Senior Member
Join Date: Dec 2006
Posts: 209
|
the mediators I am using are all based on puremvc mediator classes, which are based on an iMediator interface, so I am using those.
the classes ARE imported, if I add the case where one class is instanciated, the OTHER class suddenly works as well, even though the case with 'case EventMediator:' is never used. Back in AS2 I had one similar phenomenon, where a textfield's width was never available, until I put a trace command in the code... This one seems the same, adding a line of code that is never used, suddenly makes the other code work.
|
|
|
11-02-2009, 10:18 PM
|
#7
|
|
Senior Member
Join Date: Dec 2006
Posts: 209
|
and the basicmediator IS created, too before I check for it. When I trace it, then there IS a BasicMediator instance. It throws the error after.
|
|
|
11-03-2009, 06:06 AM
|
#8
|
|
Holosuit User
Join Date: Oct 2006
Location: Tel Aviv
Posts: 3,833
|
I'm sorry to tell you, but there isn't a chance these two happen at the same time:
Quote:
|
Error #1065: Variable BasicMediator is not defined
|
and
By saying "imported" I mean exactly the code I posted before, i.e. import + use at least once. Linker doesn't write the class definition into SWF if it's formally imported but never used. Besides, well, install some decompiler, or just check which classes you have compiled into your SWF, you can do that in Flex, FD or just uncompress the SWF and look up the classes declaration part - it's just UTF-8 text, so, you don't need any special software to see that.
|
|
|
11-03-2009, 10:10 AM
|
#9
|
|
Senior Member
Join Date: Dec 2006
Posts: 209
|
ah now I am getting more what you are at.
even though I used an 'import' line in my script, the class isn't imported unless I specifically have the class name in my code somewhere.(which is exactly what I want to avoid.
But that explains the fact that once I write 'EventMediator' in my script, the whole thing works, because EventMediator is mentioned and it is inheriting BasicMediator, therefore both mediator classes are available.
But that sucks, as I should be a bit more in power on what classes I import as well, no?
So is there a way to tell my compiler that, I want e.g. all classes in myproject.mediators.* to be added to the compiled app, without mentioning there names. The benefit of this would be, regardless of my combination of mediators, it would always have all mediators of one package included in the app.
I can do this with movieclips and other library assets, can I do that with classes, too?
|
|
|
11-03-2009, 12:58 PM
|
#10
|
|
French Error ...
Join Date: Dec 2007
Location: Vermont, USA
Posts: 5,055
|
No you can't and you might think it's a disadvantage (when you import a class, class ARE imported on many other languages) but personally I think it's pretty handy. So yes you need to reference the class and not just import it like wvxvw said:
ActionScript Code:
import EventMediator;//import
EventMediator;//reference
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 03:16 AM.
|
|