Version: Smart Feature Phone 2.5

DataStore.add()

The add() method of the DataStore interface adds a new record to the data store; if the record you are attempting to add already exists, it will throw an exception.

Syntax#

store.add(object).then(function(id) {
// Do something with id, which is the id of the record added to the data store
});

Returns#

A Promise object of type DataStoreKey that resolves with a ID number identifying the record added to the store.

Parameters#

  • object
    The object you want to add to the data store.
  • id
    You can specify an optional second parameter that will be a DataStoreKey of type unsigned long or DOMString, if you want to give the added record a specific id. If you don't specify this, the object's id will be numeric (auto incremented).
  • revisionId
    You can specify an optional third parameter that will be a revisionId (a DOMString). This can be used to prevent conflicts. If the revisionId is not the current revisionId for the current Data Store, the operation is aborted. This means that the developer has a 'old' revisionId and will have to manage the conflict somehow.

Example#

The following example function takes the current data store and an object to be added to the data store as arguments, adds the object to the store, and then creates a table row to display the object data in the app and appends it to an existing table in the DOM:

function addContact(store, obj) {
store.add(obj).then(function(id) {
var tableRow = document.createElement('tr');
tableRow.innerHTML = '<td>' + obj.lName +
'</td><td>' + obj.fName +
'</td><td>' + obj.phone +
'</td><td>' + obj.email +
'</td><td>' + obj.address + '</td>';
tBody.appendChild(tableRow);
var myId = id;
console.log(myId);
tableRow.setAttribute('data-id', myId);
tableRow.onclick = function(event) {
deleteItem(event);
}
});
}