如何返回嵌套函数的值

时间:2013-07-10 13:58:31

标签: javascript openbravo

如何从以下javascript函数中获取返回对象 -

function find(model, whereClause, success, error, args) {
    var tableName = model.prototype.tableName,
        propertyMap = model.prototype.propertyMap,
        sql = 'SELECT * FROM ' + tableName,
        params = null,
        appendWhere = true,
        firstParam = true,
        k, v;

    if (db) {
      // websql
      if (whereClause && whereClause._whereClause) {
        whereClause.sql = ' ' + whereClause._whereClause;
      } else {
        whereClause = getWhereClause(whereClause, propertyMap);
      }
      sql = sql + whereClause.sql;
      params = whereClause.params;

      if (model.prototype.propertyMap._idx) {
        sql = sql + ' ORDER BY _idx ';
      }

      if (model.prototype.dataLimit) {
        sql = sql + ' LIMIT ' + model.prototype.dataLimit;
      }

      db.readTransaction(function (tx) {
        tx.executeSql(sql, params, function (tr, result) {
          var i, collectionType = OB.Collection[model.prototype.modelName + 'List'] || Backbone.Collection,
              collection = new collectionType(),
              len = result.rows.length;
          if (len === 0) {
            success(collection, args);
          } else {
            for (i = 0; i < len; i++) {
              collection.add(transform(model, result.rows.item(i)));
            }
            if(success){
             success(collection, args);
            } else {
             return collection
            }
          }
        }, error);
      });
    } else {
      // localStorage
      throw 'Not implemented';
    }
  }

我希望在调用函数find()时返回db.readTransaction的tx.executeSql()中的变量集合,如

OB.Dal.find(OB.Model.OutletSection, null, null, null, null)

1 个答案:

答案 0 :(得分:1)

你应该像这样调用函数find:

OB.Dal.find(OB.Model.OutletSection, null, function(collection, args){

    alert('Collection variable: '+collection);

}, null, null);