IndexedDB之鍵值范圍(Key Ranges)
Key Ranges 鍵值范圍
Working with cursors may seem suboptimal since you’re limited in the ways data can be retrieved. Key ranges are used to make working with cursors a little more manageable. A key range is represented by an instance of IDBKeyRange. The standard version is IDBKeyRange, which is supported in Internet Explorer 10+ and Firefox 4+, while Chrome supports this type with webkitIDBKeyRange. As with other types related to IndexedDB, you’ll first need to create a local copy, taking these differences into account:
翻譯:
使用光標(biāo)似乎有點不完美,因為你被限制在了數(shù)據(jù)取出的方式里。鍵值范圍的使用讓光標(biāo)更加可控。一個鍵值范圍是IDBKeyRange的一個實例。IDBKeyRange是標(biāo)準(zhǔn)版本,IE10+和FF4+支持,Chrome支持webkitIDBKeyRange。和其他IndexDB的類型一樣,你首先需要聲明一個兼容的本地版本:
var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
There are four different ways to specify key ranges. The first is to use the only() method and pass in the key you want to retrieve:
翻譯:
有四種方式來指定鍵值范圍。第一個方法是only(),傳入你想要取的數(shù)據(jù)的鍵值。
var onlyRange = IDBKeyRange.only(“007”);
This range ensures that only the value with a key of “007” will be retrieved. A cursor created using this range is similar to directly accessing an object store and calling get(“007”).
翻譯:
這個鍵值范圍確保只有鍵值為“007”的對象被取出。使用這種鍵值范圍創(chuàng)建的光標(biāo)和直接訪問一個對象存儲然后調(diào)用get("007")是相似的。
The second type of range defines a lower bound for the result set. The lower bound indicates the item at which the cursor should start. For example, the following key range ensures the cursor starts at the key “007” and continues until the end:
翻譯:
鍵值范圍的第二種創(chuàng)建方法是為結(jié)果集設(shè)置一個下限。下限指明了光標(biāo)的起始條目。例如,下面的鍵值范圍確保了光標(biāo)開始于鍵值"007",然后直到結(jié)束。
//start at item “007”, go to the end
var lowerRange = IDBKeyRange.lowerBound(“007”);
If you want to start at the item immediately following the value at “007”, then you can pass in a second argument of true:
翻譯:
假如你想要沖鍵值"007"之后一個條目開始,那么傳第二個參數(shù)為true:
//start at item after “007”, go to the end
var lowerRange = IDBKeyRange.lowerBound(“007”, true);
The third type of range is an upper bound, indicating the key you don’t want to go past by using the upperBound() method. The following key ensures that the cursor starts at the beginning and stops when it gets to the value with key “ace”:
翻譯:
第三種是設(shè)置上限。通過方法upperBound()標(biāo)明你不想經(jīng)過的key。下面的鍵值確保光標(biāo)從頭開始然后當(dāng)遇到鍵值"ace"時停止。
//start at beginning, go to “ace”
var upperRange = IDBKeyRange.upperBound(“ace”);
If you don’t want to include the given key, then pass in true as the second argument:
假如你不想包含給定的鍵值,那么第二個參數(shù)傳true:
//start at beginning, go to the item just before “ace”
var upperRange = IDBKeyRange.upperBound(“ace”, true);
To specify both a lower and an upper bound, use the bound() method. This method accepts four arguments, the lower bound key, the upper bound key, an optional Boolean indicating to skip the lower bound, and an optional Boolean indicating to skip the upper bound. Here are some examples:
翻譯:
如果要同時指定上限和下限,那么使用方法bound()。這個方法接受四個參數(shù),下限鍵值,上限鍵值,可選布爾值指明是否跳過下限,和可選布爾值指明是否跳過上限。這是一些例子:
//start at “007”, go to “ace”
var boundRange = IDBKeyRange.bound(“007”, “ace”);
//start at item after “007”, go to “ace”
var boundRange = IDBKeyRange.bound(“007”, “ace”, true);
//start at item after “007”, go to item before “ace”
var boundRange = IDBKeyRange.bound(“007”, “ace”, true, true);
//start at “007”, go to item before “ace”
var boundRange = IDBKeyRange.bound(“007”, “ace”, false, true);
Once you have defined a range, pass it into the openCursor() method and you’ll create a cursor that stays within the constraints:
一旦你定義了一個鍵值范圍,并且把他傳遞進了方法openCursor(),那么你將創(chuàng)建一個停留在指定限制的光標(biāo)。
var store = db.transaction(“users”).objectStore(“users”), range = IDBKeyRange.bound(“007”, “ace”);
request = store.openCursor(range);
request.onsuccess = function(event){
var cursor = event.target.result;
if (cursor){ //always check
console.log(“Key: “ + cursor.key + “, Value: “ + JSON.stringify(cursor.value));
cursor.continue(); //go to the next one } else { console.log(“Done!”); } };
This example outputs only the values between keys “007” and “ace”, which are fewer than the previous section’s example.
翻譯:
這個例子只輸出介于"007"和"ace"的值,它比上一章節(jié)的例子少很多。