2012-08-25 23 views
5

मुझे पता है कि IDBObjectStore.getAll is not part of the IndexedDB standard और । लेकिन इसे फ़ायरफ़ॉक्स में कार्यान्वित किया गया है, और यदि आपको डेटाबेस से बहुत सारी ऑब्जेक्ट्स पुनर्प्राप्त करना पड़ता है तो यह आपके कोड को सुंदर बनाता है।इंडेक्सड डीबी गैर-फ़ायरफ़ॉक्स ब्राउज़र में सभी

क्या इंडेक्स डीडी का समर्थन करने वाले अन्य ब्राउज़रों में काम करने के लिए getAll को अनुमति देने के लिए कुछ प्रकार का पॉलीफिल या कुछ बनाना संभव होगा? getAll की वास्तविक कार्यक्षमता सरल है, लेकिन मुझे नहीं पता कि इंडेक्सड डीबी की असीमित प्रकृति से कैसे निपटें, गैर-फ़ायरफ़ॉक्स ब्राउज़र में इसके सटीक वाक्यविन्यास को दोहराने के संदर्भ में।

उत्तर

10

मैंने a GitHub repo for a shim to support getAll in other browsers बनाया, जो क्रोम में काफी अच्छी तरह से काम करता है। कोड को पोस्टरिटी के लिए नीचे दोहराया गया है:

(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

नमस्ते, भविष्य में 4 वर्षों से बात करते हुए हाय; इसे "मैं आपको पहले अनुमान लगाने देता हूं ..." में काम करने के लिए अपने कोड की आवश्यकता है, yessss, इंटरनेट एक्सप्लोरर (क्लैप क्लैप) – sergio0983

+1

लॉल आईई/एज अभी भी बहुत पीछे है। – user1133275