Question

I'm new to flex and I can't seem to get this to work. Essentially, I have a select box with which it's available data is dependent on another combo box.

There are multiple TreatmentTypes in each CategoryType.

Here's my code:

Combo box change; update select box:

private function refreshAvailableTreatmentTypes():void {
        // this is the combo box
        fAccomplishment.habitatType = habitatTypeId.selectedIndex != -1 ? habitatTypeId.selectedItem as HabitatType : null; 
        // fAccompRemote is a RemoteObject
        var treatmentList:ArrayCollection = fAccompRemote.getValidTreatments(fAccompForm.accomplishment, fAccomplishment.habitatType);

        if ( fAccompForm.categoryTypes != null ) {
            // All categories are always shown. These are passed to the form on construction. 
            for each ( var currentCat:CategoryType in fAccompForm.categoryTypes ) {
                var catAdded:Boolean = false;
                /* loop through all the treatments in each Category and add them to 
                 * the available list if they meet the criteria */
                for each ( var currentTreat:TreatmentType in currentCat.treatments ) {
                    if (currentTreat.id in treatmentList || treatmentList.length == 0) {
                        if (!catAdded) {
                            // fCatsAndTreats defined as a [Bindable] private var
                            fCatsAndTreats.addItem( currentCat );
                            catAdded = true;
                        } 

                        fCatsAndTreats.addItem( currentTreat );
                    }
                }
            }
        }
    }

Service Method:

@RemotingInclude
public List<TreatmentType> getValidTreatments(Accomplishment accomp, HabitatType selectedHabitatType){
    if ( accomp == null || accomp.getGeometry() == null || accomp.getHabitatType() == null) {
        return new ArrayList<TreatmentType>();
    }

    Geometry accompGeo = accomp.getGeometry();
    List<TreatmentType> optionList = new ArrayList<TreatmentType>();
    String geomName = null;

    if ( accompGeo instanceof Point || accompGeo instanceof MultiPoint ) {
        geomName = "Point";
    } else if ( accompGeo instanceof LineString || accompGeo instanceof MultiLineString) {
        geomName = "Line";
    } else if ( accompGeo instanceof Polygon || accompGeo instanceof MultiPolygon ) {
        geomName = "Polygon";
    }

    Integer habTypeId = null;
    if (selectedHabitatType == null) {
        habTypeId = accomp.getHabitatType().getId();
    } else {
        habTypeId = selectedHabitatType.getId();
    }
    optionList = accomplishmentDao.getValidTreatments(geomName, habTypeId);

    return optionList;
}

TypeError: Error #1034: Type Coercion failed: cannot convert mx.rpc::AsyncToken@1af48641 to mx.collections.ArrayCollection.

How do I do this? I found THIS but it doesn't seem to help me quite so much: . Any resources or information would be greatly appreciated.

Was it helpful?

Solution

Calls to RemoteObject are asynchronous - the return value from fAccompRemote.getValidTreatments is an AsyncToken, which defines how the results (when they are returned) will be handled.

When the remote call returns, it will invoke either the result handler or the fault handler, depending on if the call was successful or had a fault.

There are a couple of different ways you can set up the response handler for your code - you can either addEventListener on the call, or set the responder in the AsyncToken returned from the call.

fAccompRemote.addEventListener(ResultEvent.RESULT, resultHandler);
fAccompRemote.getValidTreatments(...)

-or-

var token:AsyncToken = fAccompRemote.getValidTreatments(...);
token.addResponder(new AsyncResponder(resultHandler, faultHandler));

In either case, resultHandler receives a ResultEvent event, with the ArrayCollection that you returned from getValidTreatments

protected function resultHandler(event:ResultHandler):void
{
    var results:ArrayCollection = event.result as ArrayCollection;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top