Flex - Cross domain objects
I have an object being passed between flash and flex using the a custom event. I am importing a library in flex containing a copy of the object's class. The classes are identical, but when I attempt to access the object in flex I get this error:
TypeError: Error #1034: Type Coercion failed: cannot convert com.cackleberries.data.api::ApiObject$ to com.cacklebe开发者_Go百科rries.data.api.ApiObject.
Any ideas?
This function is passed into flash as a callback from AIR / flex
public function airEventHandler(type:String, data:Object):void
{
switch(type)
{
case "air_api_call":
if(data)
{
if(data.hasOwnProperty("apiObject"))
{
trace("got air api call event in application");
serverApi.makeApiCall(ApiObject(data.apiObject));
}
}
break;
}
}
I am getting the error when I pass the ApiObject
to serverApi.makeApiCall
. That function takes an ApiObject
as its parameter. Initially, the data object is created with with the apiObject
key with a ApiObject
as the value (done in flash).
The problem's not with serverApi.makeApiCall
, but with the cast of data.apiObject
to ApiObject
.
The reason you're getting this error is because data.apiObject
is of the type ApiObject
included in the flash app, while the flex app is using ApiObject
as included into itself.
Even though the source files are identical, to the player they apparently are not. I think this is because the flex compiler doesn't compile in exactly the same way as the flash compiler, but I'm not 100% on that.
Anyway, to solve this you should use interfaces. Let ApiObject
implement an IApiObject
interface with all the necessary methods (ie. the methods the flex app will need) defined in it and then on the flex side cast data.apiObject
to IApiObject
instead of ApiObject
.
精彩评论