View comments | RSS feed

Handling function results in ActionScript

Handling function results in ActionScript is the same for Java application servers and the other platforms that Flash Remoting MX supports. You write an ActionScript result handler function with a name composed of the name of the function returning the result with _Result appended to it. For example, to pass the return value of the following ActionScript function to ActionScript:

function calculate()
{
  loanService.calculate( (number (principalInput.text)), (number (monthsInput.text)), (number (rateInput.text)) );
}

You would use the following ActionScript function:

function calculate_Result( result )
{
  payOutput.text = result;
}

If a method returns an object that implements the Java Serializable interface, and all of its fields are serializable, its public and private properties are available as ActionScript properties. For example, the following method in a Java class called Loan calculates loan payments and returns an instance of a JavaBean called LoanInfo that stores principal, months, rate, and monthlyPayment property values:

public LoanInfo calculateReturnComplex(double principal, int months, float rate){
    if (rate < 0 || rate>1)
      principal = 0.0;
    double monthlyPayment = principal * (rate / (1 - Math.pow(1 + rate,-months)));
    LoanInfo info=new LoanInfo(principal, months, rate, monthlyPayment);
    return info;
  }

You could use the following function to call this method in ActionScript:

function calculate()
{
  loanService.calculateReturnComplex( (number (principalInput.text)), (number (monthsInput.text)), (number (rateInput.text)) );
}

The calculate function returns the following LoanInfo bean:

package samples;
public class LoanInfo {
  private double principal;
  private int months;
  private float rate;
  private double monthlyPayment;
  public LoanInfo() {
  }
  public LoanInfo(double principal, int months, float rate, double monthlyPayment) {
    this.principal=principal;
    this.months=months;
    this.rate=rate;
    this.monthlyPayment=monthlyPayment;
  }
        public LoanInfo(double principal, int months, float rate, double monthlyPayment, String message) {
    this.principal=principal;
    this.months=months;
    this.rate=rate;
    this.monthlyPayment=monthlyPayment;
                this.message=message;
  }
  public void setPrincipal(double principal) {
    this.principal=principal;
  }
  public double getPrincipal() {
    return principal;
  }
  public void setMonths(int months) {
    this.months=months;
  }
  public int getMonths() {
    return months;
  }
  public void setRate(float rate) {
    this.rate=rate;
  }
  public float getRate() {
    return rate;
  }
  public void setMonthlyPayment(double monthlyPayment) {
    this.monthlyPayment=monthlyPayment;
  }
  public double getMonthlyPayment() {
    return monthlyPayment;
  }
}

You can use the following ActionScript code to get the value of the LoanInfo bean's MonthlyPayment property:

function calculate_Result (result)
{
  payOutput.text = result.monthlyPayment;
}

For detailed information about the ActionScript result-handling hierarchy and result-handling strategies, see "Handling service results," in Chapter�2.

Comments


No screen name said on Mar 22, 2004 at 8:48 AM :
I don't know why result.monthlyPayment will not return an error! monthlyPayment is private so we can access it by the method. How it works? Maybe I missed something!
No screen name said on Mar 22, 2004 at 8:53 AM :
I need something important. Is it possible to handle a java collection like a map or a list? If yes, how can we do it? How it's possible to iterate with the list. Thanks!

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flashremoting/mx/Using_Flash_Remoting_MX/usingFRJ2EE8.htm