You can strongly type CF objects that are returned through RemoteObject
calls prior to arriving in Flex...

Take the following example:

Using a CFC called Animal... this is like a VO or DTO class which defines a
data object instance...

<cfcomponent output="false" alias="Animal">
    <cfproperty name="AnimalID" type="string" default="">
    <cfscript>
        this.AnimalID = "";
    </cfscript>
    <cffunction name="init" output="false" returntype="Animal">
        <cfreturn this>
    </cffunction>
    <cffunction name="getAnimalID" output="false" access="public"
returntype="any">
        <cfreturn this.AnimalID>
    </cffunction>
    <cffunction name="setAnimalID" output="false" access="public"
returntype="void">
        <cfargument name="val" required="true">
        <cfset this.AnimalID = arguments.val>
    </cffunction>
</cfcomponent>


<cffunction name="getArrayOfAnimals" access="remote" returntype="array"
output="Yes">

                <cfset returnArray = #ArrayNew(1)# />

                <!--- Getting the comments for this annotation --->
                <cfstoredproc procedure="GetAnimals"
datasource="#datasource#">
                    <cfprocresult name="animalResults" />
                </cfstoredproc>

                <!--- Loops through the query returned from the stored
procedure call, and allows us to create a strongly type object instance with
the query results for return to Flex --->
                <cfloop query="animalResults">
                    <cfset newAnimal = '' />
                    <cfscript>
                        //creating our new animal of type Animal from the
CFC above
                        newAnimal = createObject("component", "Animal");
                        newAnimal .setAnimalID(userResultSet.ANIMALID);
//Assumes your return property from the query is AnimalID -- must be
capitalized
                        ArrayAppend(returnArray, newAnimal );
                    </cfscript>
                </cfloop>

                <cfreturn #returnArray# />

    </cffunction>


This method will return an array of Animal objects which get serialized into
Flex as such.  You'll need to create a value object class in Flex of this
type, and make sure that the RegisterClassAlias that Alex points out is set
properly on it.  There's quite a bit of documentation on this, and wizards
(if you install the CFConnectivity) in FlexBuilder to ease the creation of
objects from AS to CFC's.



Brendan




On Nov 9, 2007 8:03 PM, ben.clinkinbeard <[EMAIL PROTECTED]> wrote:

>   Good point Alex, I guess conversion does occur when dealing with
> primitives. As for there being no casting in AS, how would you
> describe what happens with complex types? Like dog = Dog(animalArg)
> from below.
>
> Ben
>
>
> --- In [email protected] <flexcoders%40yahoogroups.com>, "Alex
> Harui" <[EMAIL PROTECTED]> wrote:
> >
> > Excellent description of casting, but IMHO, there is no casting in AS.
> > Using a function to 'cast' like Number("5") actually does do a
> > conversion in some cases as does 5.5 as int
> >
> >
> >
> > To the original question, your data should be converted to Position
> > objects automatically if you got set up correctly. I'm not an expert in
> > that area, but RegisterClassAlias should be part of the solution.
> >
> >
> >
> > ________________________________
> >
> > From: [email protected] <flexcoders%40yahoogroups.com> [mailto:
> [email protected] <flexcoders%40yahoogroups.com>] On
> > Behalf Of ben.clinkinbeard
> > Sent: Friday, November 09, 2007 12:29 PM
> > To: [email protected] <flexcoders%40yahoogroups.com>
> > Subject: [flexcoders] Re: Casting in flex
> >
> >
> >
> > Yes, casting does not actually convert anything from one type to
> > another. Casting simply allows you to refer to an object by a type
> > that it already is, but that its not specifically defined as.
> >
> > I know that sounds confusing, so let me explain a bit. You could have
> > a method that accepted an argument of type Animal, because you want to
> > be able to send any subtype of Animal to the function, such as Dog,
> > Cat, Fish, etc. Inside that function, if you wanted to call methods
> > that were specific to the subclass passed in, you could do a check and
> > then a cast. So maybe the method body would look like this:
> >
> > animalArg.feed();
> >
> > // specific actions
> > if(animalArg is Dog){
> > var dog:Dog = Dog(animalArg);
> > dog.walk();
> > }
> > else if(animalArg is Fish){
> > var fish:Fish = Fish(animalArg);
> > fish.flush();
> > }
> >
> > Also, I don't think the as operator is exactly the same as a cast but
> > its very similar and I don't feel like looking up the difference right
> > now. My suggestion would be to convert your objects when the result is
> > returned and then not have to worry about casting them later. You can
> > just make the Position class constructor accept an Object and pull the
> > properties off of it.
> >
> > HTH,
> > Ben
> >
> > --- In [email protected] <flexcoders%40yahoogroups.com><mailto:
> flexcoders%40yahoogroups.com>
> > , "luciddream54321" <asoltys@> wrote:
> > >
> > > Hi,
> > >
> > > I'm working with Arash on this project. Thanks for the suggestions
> > > but even using these alternate methods for geting the selectedItem the
> > > cast to a Position object still fails.
> > >
> > > Our dataProvider is an ArrayCollection that is populated by a
> > > ColdFusion query returned from a RemoteObject call. The query rows
> > > are from our positions table but flex is storing the rows as generic
> > > 'Objects' within the ArrayCollection. Here is the line we use in our
> > > RemoteObject result handler:
> > >
> > > positions = event.result as ArrayCollection;
> > >
> > > Using the debugger we've been able to verify that the Objects in the
> > > ArrayCollection do have the same public properties as our Position
> > > value object.
> > >
> > > So is it necessary to convert the query rows into Position objects
> > > immediately when they come back from ColdFusion rather than casting
> > > them after we get them out of the ArrayCollection? How would this be
> > > done exactly?
> > >
> > > Adam
> > >
> > > --- In [email protected] <flexcoders%40yahoogroups.com>
> > <mailto:flexcoders%40yahoogroups.com> , "ben.clinkinbeard"
> > > <ben.clinkinbeard@> wrote:
> > > >
> > > > I assume your dataProvider is a collection of Position objects? Is
> > it
> > > > an ArrayCollection?
> > > >
> > > > Try changing this line
> > > >
> > > > selectedItem = event.currentTarget.dataProvider[selectedRow];
> > > >
> > > > to this
> > > >
> > > > selectedItem =
> > event.currentTarget.dataProvider.getItemAt(selectedRow);
> > > >
> > > > HTH,
> > > > Ben
> > > >
> > > >
> > > > --- In [email protected] <flexcoders%40yahoogroups.com>
> > <mailto:flexcoders%40yahoogroups.com> , "arashafrooze" <arash.afrooze@>
>
> > > > wrote:
> > > > >
> > > > > Hi,
> > > > > I'm a flex newbie, so please bear with me ...
> > > > > I have an advanced datagrid with a change property that calls a
> > > > > function passing it the event.
> > > > > inside the function i retrieve the selected row and cast it as an
> > > > > object. then i try to cast this object as, what i call a Position
> > > > > object. Even though both objects are identical in properties, My
> > > > > postion object does not get populated and is assigned NULL.
> > > > > I'm pasting the code to my function.
> > > > > Any help is greatly appreciated.
> > > > >
> > > > >
> > > > > public function updateSelectedPosition(event:Event):void
> > > > > {
> > > > > var position:Position = new Position();
> > > > > var selectedItem:Object;
> > > > > var selectedRow:uint = event.target.selectedCells[0].rowIndex;
> > > > > selectedItem = event.currentTarget.dataProvider[selectedRow];
> > > > > position = selectedItem as Position;
> > > > > }
> > > > >
> > > > > Thank you all so very much in advance :D
> > > > >
> > > >
> > >
> >
>
>  
>



-- 
Brendan Meutzner
http://www.meutzner.com/blog/

Reply via email to