2013-03-18 88 views
1
我有执行多个成功事件中的成功功能的问题

,让我告诉你的代码:jQuery Mobile的数据库成功事件不会触发功能

$("#beneficiarios").one("pagebeforeshow", function(){ 

    db.transaction(function(tx){ queryDBAll(tx, "Beneficiarios", 0, 10) }, onError); //LOAD BENEFICIARIOS IN DB 

    $("#btn-add-ben").click(function(){ 
     var nombres = $('#b_nombres').val(); 
     var apellidos = $('#b_apellidos').val(); 
     var dia = $('#b_dia').val(); 
     var mes = $('#b_mes').val(); 
     var año = $('#b_año').val(); 
     var fecha_nac = año+"-"+mes+"-"+dia; 

     //ADD BENEFICIARIO TO DB 
     db.transaction(function(tx){ 
      tx.executeSql("INSERT INTO Beneficiarios (nombres, apellidos, fecha_nac) VALUES ('"+nombres+"','"+apellidos+"','"+fecha_nac+"')", 
      [], 
      function(tx, results){ 
       var benid = results.insertId; 
       $('#emptyBeneficiarios').remove(); 
       $('#ul-beneficiarios').append(
      '<li><a href="#" id="view_ben_'+benid+'">'+nombres+' '+apellidos+'</a><a href="#" id="del_ben_'+benid+'" data-position-to="window" data-icon="delete" class="itemDelete">Eliminar</a></li>').listview('refresh'); 
       $("#del_ben_"+benid).click(function(){ 
        //PENDING: CONFIRM DIALOG 
        db.transaction(function(tx){ 
        tx.executeSql("DELETE FROM Beneficiarios WHERE ID = "+benid), 
        [], 
        console.log(benid+" borrado con éxito!"), 
        onError 
        }); 
       }); 
       $("#view_ben_"+benid).click(function(){ 
        db.transaction(function(tx){ 
        tx.executeSql('SELECT * FROM Beneficiarios WHERE ID = '+benid), 
        [], 
        //SUCCESS ACTION! -- NOT WORKING 
        function(tx, results) { 
         $('#b_nombres').val(results.rows.item.nombres); 
         $('#b_apellidos').val(); 
         $('#b_dia').val(); 
         $('#b_mes').val(); 
         $('#b_año').val(); 
         console.log(benid+" shown successfully!") //IS NOT SHOWN AT ALL 
        } 
        console.log(benid+" mostrado shown successfully without function!") //SHOWN AS DESIRED 
        , 
        onError 
        }); 
       }); 
      }, 
      onError); 
     }, 
     onError); 
     }); 

    $("#ul-beneficiarios").delegate("li a", "click", function() { 
     $(this).parent("li").remove(); 
     var li_num = $('#ul-beneficiarios li').length; 
     if (li_num == 1) { 
      addEmptyBeneficiarios('#ul-beneficiarios'); 
     } 
     }); 
}); 

正如你可以想像我有李的UL那当单击ADD按钮时会被添加,一切正常,包括DB执行。

问题是,当我点击$(“#view_ben _”+ benid)它正常运行,但它不能正确执行SELECT成功函数,虽然它确实显示了console.log设置在成功函数之外。任何人有任何想法?

回答

0

好吧,我想通了: 在下面一行中,您直接关闭executeSql函数,忽略了函数体中未使用的其余参数。换句话说:缺少一个支架;)。

tx.executeSql('SELECT * FROM Beneficiarios WHERE ID = '+benid), 

你的语法更改为下面的例子:

db.transaction(function(tx){ 
    tx.executeSql(
     ("DELETE FROM Beneficiarios WHERE ID = "+benid), // SQL COMMAND 
     [],            // NO IDEA WHAT THAT IS FOR 
     console.log("success"),       // SUCCESS CALLBACK 
     onError           // ERROR CALLBACK 
    ); 
}); 

我想指出的是您正在恶补了过多的功能相互转化。考虑将绑定,成功和错误功能外包到顶层,以防止像Matryoshka代码这样的Inception。