Guest User

Untitled

a guest
May 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. function createAndUpdateDB() {
  2. 'use strict';
  3.  
  4. //check for support
  5. if (!('indexedDB' in window)) {
  6. console.log('This browser doesn\'t support IndexedDB');
  7. return;
  8. }
  9.  
  10. var dbPromise = idb.open('restaurant-reviews', 1, function(upgradeDb){
  11. if (!upgradeDb.objectStoreNames.contains('restaurants')) {
  12. upgradeDb.createObjectStore('restaurants', {keyPath: 'id'});
  13. }
  14. });
  15.  
  16. var items;
  17. DBHelper.fetchRestaurants( (error, restaurants) => {
  18. if(error){
  19. console.log(error);
  20. } else {
  21. items = restaurants;
  22. }
  23. });
  24.  
  25. dbPromise.then(function(db) {
  26. var tx = db.transaction('restaurants', 'readwrite');
  27. var store = tx.objectStore('restaurants');
  28. items.forEach(item => {
  29. store.put(item);
  30. });
  31. return tx.complete;
  32. }).then(function() {
  33. console.log('Store Updated');
  34. });
  35. }
Add Comment
Please, Sign In to add comment