2012-08-25 19 views
5

Tôi biết rằng IDBObjectStore.getAll is not part of the IndexedDB standard và rằng . Nhưng nó được thực hiện trong FireFox, và nó làm cho mã của bạn đẹp hơn nếu bạn phải lấy rất nhiều đối tượng từ cơ sở dữ liệu.Chỉ mụcDB getAll trong các trình duyệt không phải là Firefox

Có thể thực hiện một số loại hình đa giác hoặc một thứ gì đó để cho phép getAll hoạt động trong các trình duyệt khác hỗ trợ IndexedDB không? Các chức năng thực tế của getAll là đơn giản, nhưng tôi không biết làm thế nào để đối phó với bản chất không đồng bộ của IndexedDB trong bối cảnh sao chép cú pháp chính xác của nó trong các trình duyệt không phải Firefox.

Trả lời

10

Tôi đã thực hiện a GitHub repo for a shim to support getAll in other browsers, dường như hoạt động tốt trong Chrome. Mã được lặp lại bên dưới cho hậu thế:

(function() { 
    "use strict"; 

    var Event, getAll, IDBIndex, IDBObjectStore, IDBRequest; 

    IDBObjectStore = window.IDBObjectStore || window.webkitIDBObjectStore || window.mozIDBObjectStore || window.msIDBObjectStore; 
    IDBIndex = window.IDBIndex || window.webkitIDBIndex || window.mozIDBIndex || window.msIDBIndex; 

    if (typeof IDBObjectStore.prototype.getAll !== "undefined" && typeof IDBIndex.prototype.getAll !== "undefined") { 
     return; 
    } 

    // https://github.com/axemclion/IndexedDBShim/blob/gh-pages/src/IDBRequest.js 
    IDBRequest = function() { 
     this.onsuccess = null; 
     this.readyState = "pending"; 
    }; 
    // https://github.com/axemclion/IndexedDBShim/blob/gh-pages/src/Event.js 
    Event = function (type, debug) { 
     return { 
      "type": type, 
      debug: debug, 
      bubbles: false, 
      cancelable: false, 
      eventPhase: 0, 
      timeStamp: new Date() 
     }; 
    }; 

    getAll = function (key) { 
     var request, result; 

     key = typeof key !== "undefined" ? key : null; 

     request = new IDBRequest(); 
     result = []; 

     // this is either an IDBObjectStore or an IDBIndex, depending on the context. 
     this.openCursor(key).onsuccess = function (event) { 
      var cursor, e, target; 

      cursor = event.target.result; 
      if (cursor) { 
       result.push(cursor.value); 
       cursor.continue(); 
      } else { 
       if (typeof request.onsuccess === "function") { 
        e = new Event("success"); 
        e.target = { 
         readyState: "done", 
         result: result 
        }; 
        request.onsuccess(e); 
       } 
      } 
     }; 

     return request; 
    }; 

    if (typeof IDBObjectStore.prototype.getAll === "undefined") { 
     IDBObjectStore.prototype.getAll = getAll; 
    } 
    if (typeof IDBIndex.prototype.getAll === "undefined") { 
     IDBIndex.prototype.getAll = getAll; 
    } 
}()); 
+1

Xin chào, nói từ 4 năm tới trong tương lai; cần mã của bạn để làm cho nó hoạt động trong "Tôi cho phép bạn đoán đầu tiên ..." yessss, internet explorer (vỗ tay vỗ tay) – sergio0983

+1

lol IE/Edge vẫn còn xa phía sau. – user1133275

Các vấn đề liên quan