Ajax IndexedDB删除当前成功全部上传 [英] Ajax IndexedDB Delete Current Sucesfull Upload

查看:105
本文介绍了Ajax IndexedDB删除当前成功全部上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天发布了类似的内容,但是它可以正常工作,但是只删除了数据中的最后一个对象.

I posted something similar yesterday but it works but only deleted the last object in the data.

我想发生的事情

此ajax上传将处理大量数据,因此我正在使用indexeddb.这也将在手机上使用.因此,我希望它一次上传一个项目,如果一个项目未能从数据中删除之前的项目,那么他们就不需要再次上传所有内容.

This ajax upload will be handling a lot of data, so I'm using indexeddb. This will also be use on mobile phones. So I wanted it to upload one item at a time and if one item failed to have only deleted the previous items out of the data so they wouldn't need to upload everything again.

我尝试了async = false,这完全符合我的需要,但此冰柜浏览器却可以.

I have tried async = false, This works exactly how i want it but this freezers browser.

当前代码试图注释掉任何可能引起混淆的位,目前,这仅在完成后才删除最后一项.

Current Code Tried to comment out any bits that could be confusing, currently this only deletes the last item once finished.

function uploadData(e) {

 //Get Database 
 var transaction = db.transaction(["data"], "readonly");
 var objectStore = transaction.objectStore("data");
 var cursor = objectStore.openCursor();

 //Starts Looping
 cursor.onsuccess = function(e) {
     var res = e.target.result;
     if (res) {
         if (navigator.onLine) {
             $('.popup-heading').text('Uploading...');
             var passData = {
                 client_id: res.value.client_id,
                 parent_id: res.value.parent_id,
                 storename: res.value.storename,
                 image: res.value.image,
                 key: res.key,
             };
             var jsonData = JSON.stringify(passData);
             $.ajax({
                 url: "{{ path('destination_app_ajax') }}",
                 type: "post",
                 // Works but freezes browser 
                 /*async, flase*/
                 data: {
                     "json": passData
                 },
                 success: function(JsonData) {

                     //Delete item once successfull 
                     var t = db.transaction(["data"], "readwrite");
                     var request = t.objectStore("data").delete(passData.key);
                     t.oncomplete = function(event) {
                         console.log('item deleted');
                     };
                 },
                 error: function() {
                     $('.popup-heading').text('Upload Failed!');
                 }
             });
         } else {
             $('.popup-heading').text('Please find stronger signal or wifi connection');
         }
         res.
         continue ();
     }
 }

}

推荐答案

这听起来像是passData的作用域问题.在循环内部,但在定义var passData = ...之前,请尝试使用匿名函数包装代码块:

It sounds like you have a scope issue with passData. Inside of your loop, but before you defined var passData = ... try wrapping the codeblock with an anonymous function:

(function() {
   /* Your code here */
}());

这应该防止passData泄漏到全局范围内,这似乎就是您的IDB代码仅在最后一个循环上起作用的原因. (每次AJAX响应完成之前,都会重新定义passData.)

That should prevent passData from leaking into the global scope, which seems to be why your IDB code only works on the last loop. (passData is being redefined each time before your AJAX response is able to complete.)

更新:没有循环,您正在处理回调.我看到的是,您在每个Ajax请求上重新定义了onsuccess处理程序(并覆盖了除最后一个值之外的所有值),重新使用了相同的事务.尝试将此事务代码移到AJAX请求的成功回调中:

Update: There is no loop, you're dealing with callbacks. What I see happening is that you're redefining your onsuccess handler on each Ajax request (and overwriting all values but the last), reusing the same transaction. Try moving this transaction code into the success callback for the AJAX request:

//Get Database 
 var transaction = db.transaction(["data"], "readonly");
 var objectStore = transaction.objectStore("data");
 var cursor = objectStore.openCursor();

这将创建一个关闭并在每个响应上提交您的delete事务.这意味着每个AJAX请求一个事务,每个AJAX请求一个onsuccess回调(不重新定义).

That will create a closure and commit your delete transaction on each response. That means one transaction per AJAX request, and one onsuccess callback per AJAX request (with no redefining).

这篇关于Ajax IndexedDB删除当前成功全部上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆