0

I am trying to do simple application with Indexed DB. I want to store multiple items in one objectStore. Is it possible? I tried something like that, but it does not work:

    itemsDB.indexedDB.addTodo = function(Name, Desp) {
    var db = itemsDB.indexedDB.db;
    var trans = db.transaction(['items'], IDBTransaction.READ_WRITE);
    var store = trans.objectStore("items");

    var data = {
      "name": Name,
      "description": Desp, 

    };

    var request = store.put(data);
   } 

I have used sample from http://www.html5rocks.com/en/tutorials/indexeddb/todo/

Peter Aron Zentai
  • 11,482
  • 5
  • 41
  • 71
Ali Ismayilov
  • 1,707
  • 3
  • 22
  • 37

2 Answers2

3

Targetting "just" IndexedDB will narrow down your compatible clients to chrome and firefox users. Have a look at JayData, it supports your object store pattern with an option of fallback provider list: if the client has IndexedDB it will be used but if only WebSQL what the client has (95% of the mobile devices) then webSQL will be used.

Also the syntax is much easier for storing or retrieving, check the ToDo list example, this also shows the fallback provider option.

Peter Aron Zentai
  • 11,482
  • 5
  • 41
  • 71
1

IndexedDB object stores were designed to house multiple objects and let you cursor across their attributes.

It's not clear exactly what's causing your put issue but if you have a key on either name or description it's going to just replace an existing object rather than add a new one. You'd want to use add instead of put.

One thing to keep in mind about the HTML5Rocks examples is that currently they will only work in Chrome (which happens to have a lagging IndexedDB implementation).

FWIW, a small tip is that there's an error in your JavaScript here (extra comma after Desp var) which might prevent it from running in IE10:

var data = {
      "name": Name,
      "description": Desp, 

    };
buley
  • 28,032
  • 17
  • 85
  • 106