如何通过函数内部的回调函数返回值?

时间:2014-04-28 08:27:13

标签: actionscript-3 flex sqlite callback flash-builder4.5

我获取数据的方式是通过回调函数[processclinicResults],

并将数组Collection提供给值对象_ClinicVO。 enter image description here

我有一个问题,我无法通过getTableContent函数直接获取返回arrayCollection,我试图在

之后返回值

enter image description here

但它返回null。

有谁知道如何轻松获取阵列? 每次我使用类似的函数时,我都不想声明变量。或者我必须覆盖[clinicData]?

public function getTableContent( resultHandler:Function, faultHandler:Function = null ):void 
{
    var stmt:SQLStatement = new SQLStatement();
    stmt.sqlConnection = sqlConnection;
    stmt.text = 'SELECT * FROM Clinic;';
    stmt.itemClass = ClinicVO;
    stmt.addEventListener( SQLEvent.RESULT,
    function ( event:SQLEvent ):void {
        resultHandler.call( this, new ArrayCollection( stmt.getResult().data ) );
    });
    stmt.addEventListener( SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler );
    stmt.execute();
}

public function errorConnectingToTable(evt:SQLErrorEvent):void {
    trace("Error getting information from DB");
}

protected function processClinicResults(resultsArray:ArrayCollection=null):void {
    if (resultsArray == null) {
        trace("DB has NO data");
    //there is no data
    } else {
        clinicData = resultsArray;
    }
}

1 个答案:

答案 0 :(得分:0)

我建议你像我一样使用Flex ORMresponders来访问本地数据库:

private var entityManager:EntityManager = EntityManager.instance;       
public var LocalDBSqlConnection:SQLConnection;      
LocalDBSqlConnection = new SQLConnection();
LocalDBSqlConnection.open(LocalDBFile);
public function loadClinic(responder:IResponder):void
    {                       
        var clinics:Array = entityManager.query("SELECT * FROM Clinic") as Array;
        responder.result(new ArrayCollection(clinics));                                 
    }

某处称之为:

loadClinic( new mx.rpc.Responder(onResult, onError));

Handelers:

private function onResult(data:Object):void
        {
            YourData = data as ArrayCollection;                         
        }
private function onError(info:Object):void
        {

        }

我很抱歉,如果这不是你正在寻找的方式=)