index.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf8" />
  5. <title>Simple localForage-getItems example</title>
  6. </head>
  7. <body>
  8. <script src="../node_modules/localforage/dist/localforage.js"></script>
  9. <script src="../dist/localforage-getitems.js"></script>
  10. <script>
  11. var driverTestOrder = [
  12. localforage.INDEXEDDB,
  13. localforage.WEBSQL,
  14. localforage.LOCALSTORAGE
  15. ];
  16. localforage.setDriver(driverTestOrder).then(function() {
  17. return localforage.clear();
  18. }).then(function() {
  19. var keyValuePairs = [
  20. { key: 'user-2-todo-4', value: 'bb44ccaa4444' },
  21. { key: 'user-2-todo-3', value: 'bb33ccaa3333' },
  22. { key: 'user-2-todo-2', value: 'bb22ccaa2222' },
  23. { key: 'user-2-todo-1', value: 'bb11ccaa1111' },
  24. { key: 'user-1-todo-4', value: '44aa4444bbcc' },
  25. { key: 'user-1-todo-3', value: '33aa3333bbcc' },
  26. { key: 'user-1-todo-2', value: '22aa2222bbcc' },
  27. { key: 'user-1-todo-1', value: '11aa1111bbcc' }
  28. ];
  29. var promises = keyValuePairs.map(function(x) {
  30. return localforage.setItem(x.key, x.value);
  31. });
  32. return Promise.all(promises);
  33. }).then(function(){
  34. // return localforage.keys();
  35. // }).then(function(keys){
  36. // console.log(keys);
  37. var itemKeys = [
  38. 'user-1-todo-4',
  39. 'user-1-todo-3',
  40. 'user-2-todo-2',
  41. 'user-2-todo-1'
  42. ];
  43. var driver = localforage.driver();
  44. Promise.resolve().then(function(){
  45. var t0 = performance.now();
  46. return localforage.getItems(itemKeys).then(function(results){
  47. console.log('getItems', results);
  48. var t1 = performance.now();
  49. console.log('Completed ' + driver + ' after ' + (t1 - t0) + ' milliseconds.');
  50. });
  51. }).then(function(){
  52. if (localforage.driver() !== localforage.LOCALSTORAGE) {
  53. var t0g = performance.now();
  54. localforage.getItems.generic.call(localforage, itemKeys).then(function(results){
  55. console.log('getItemsGeneric', results);
  56. var t1g = performance.now();
  57. console.log('Completed Generic after ' + (t1g - t0g) + ' milliseconds.');
  58. });
  59. }
  60. });
  61. });
  62. // function testPlainIndexedDB() {
  63. // var results = [];
  64. // var dbInfo = localforage._dbInfo;
  65. // var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly')
  66. // .objectStore(dbInfo.storeName);
  67. // var keyRangeValue = IDBKeyRange.bound(prefix, prefix + 'uffff', false, false);
  68. // store.openCursor(keyRangeValue).onsuccess = function(event) {
  69. // var cursor = event.target.result;
  70. // if(cursor) {
  71. // results.push(cursor.value);
  72. // cursor.continue();
  73. // } else {
  74. // console.log(results);
  75. // }
  76. // };
  77. // var req = store.get('user-2-todo-1');
  78. // req.onsuccess = function() {
  79. // var value = req.result;
  80. // if (value === undefined) {
  81. // value = null;
  82. // }
  83. // console.log(value);
  84. // };
  85. // }
  86. // function testPlainWebsql() {
  87. // var dbInfo = localforage._dbInfo;
  88. // dbInfo.db.transaction(function(tx) {
  89. // tx.executeSql('SELECT * FROM ' + dbInfo.storeName, [], function(tx, resultSet){
  90. // console.log(resultSet.rows);
  91. // });
  92. // });
  93. // dbInfo.db.transaction(function(tx) {
  94. // tx.executeSql('SELECT key FROM ' + dbInfo.storeName +
  95. // ' WHERE (key LIKE ?)', [prefix + '%'], function(tx, resultSet){
  96. // console.log(resultSet.rows);
  97. // });
  98. // });
  99. // }
  100. </script>
  101. <p>
  102. Check your console log.
  103. </p>
  104. </body>
  105. </html>