javascript,将参数传递给函数内部的闭包中的回调

javascript, passing parameters to a callback inside a closure that is inside a function

本文关键字:闭包 回调 内部 函数 参数传递 javascript      更新时间:2023-09-26

我遇到了一个与db.transaction().objectStore.onsaccess有关的问题我需要取回数据来创建一个包含结果的表。

作为第一个测试,我试图在光标被完全读取后从调用中返回数据:

function readAll() {
  var resultData=[];
  var objectStore = db.transaction("employee").objectStore("employee");
  objectStore.openCursor().onsuccess = function(event, args) {                          
    var cursor = event.target.result;               
    if (cursor){
        resultData.push(cursor.value);                  
        cursor.continue();
    }else{
       return resultData;
            }             
 };
}

但这不起作用,因为indexedDB的异步特性

所以,经过一些思考,我尝试使用回调。。。

function readAll(callBackFunction) {
  var resultData=[];
  var objectStore = db.transaction("employee").objectStore("employee");
  objectStore.openCursor().onsuccess = function(event, args) {                          
    var cursor = event.target.result;               
    if (cursor){
        resultData.push(cursor.value);                  
        cursor.continue();
    }else{
        callBackFunction;
    }             
 };
}

这似乎部分起作用,因为回调将被调用,但我无法将参数传递给回调函数,因为onsuccess=function()中的parameters数组是空的(正如我所理解的,参数绑定到readAll函数),我无法在onsuccess中传递它。那么,我如何将参数传递给回调?

还有其他不同于回调的选项吗?

谢谢

更新

我的问题是,除了结果之外,我还需要传递更多的参数,比如"table_id"、"header"等。所以我需要将一些参数传递给readAll,并将这些参数传递给callback,比如:readAll(callBackFunction,"table_id","header_array")

我已经测试了以下不起作用的解决方案:

function readAll(callBackFunction) {
  var resultData=[];
  var objectStore = db.transaction("employee").objectStore("employee");
  objectStore.openCursor().onsuccess = function(event, args) {                          
    var cursor = event.target.result;               
    if (cursor){
        resultData.push(cursor.value);                  
        cursor.continue();
    }else{
        callBackFunction(parameters[1], parameters[2], resultData);
    }             
 };
}
readAll(createTable, 'table_id', ['username', 'email']);

问题是关闭内的参数数组是空的

更新2

arguments数组在闭包内部为空,但在外部为ok。如果我把回调移到闭包之外,我有参数,但没有数据。。

更新3

此外,将所有参数添加到函数中似乎不起作用。

    function readAll(callback, header, idName, classToAdd, resultData) {
                    var resultData=[];
                    var objectStore = db.transaction("employee").objectStore("employee");
                        objectStore.openCursor().onsuccess = function(event) {                          
                    var cursor = event.target.result;               
                    if (cursor){
                        resultData.push(cursor.value);
                        //alert("Name for id " + cursor.key + " is " + cursor.value.name + ", Age: " + cursor.value.age + ", Email: " + cursor.value.email);
                        cursor.continue();
                    }else{
//HERE ALL ARGUMENTS ARE EMPTY BUT resultData Exists!
                     callback(arguments[1], arguments[2], arguments[3], arguments[4], resultData);
                    }             
               };
//HERE ALL ARGUMENTS EXISTS EXCEPT resultData!
                     callback(arguments[1], arguments[2], arguments[3], arguments[4], resultData);
}

您可以调用类似的函数

callBackFunction(resultData);

以便您传递的函数接收结果。在函数声明中,应该有这样一个参数:

function callbackFunction(results) { 
   //create the table
}

你可以像一样将其传递给readAll()

readAll(callbackFunction);

编辑:

根据您的编辑。您必须在函数声明中添加额外的参数

function readAll(callBackFunction, table_id, data) {
   // ... code
  callBackFunction(table_id, data, resultData);
}

然后,您可以在createTable函数中再声明两个参数,它们将接收"table_id"answers"data"。

过了一段时间,我又回到了这个问题上,找到了一个有效的解决方案。问题是我在一次调用中初始化了objectStore。

var objectStore = db.transaction("employee").objectStore("employee");

如果我把这个电话分成两部分:

var transaction = db.transaction("employee");
var objectStore = transaction.objectStore("employee");

一切如预期。