Skip to content

Instantly share code, notes, and snippets.

@buley
Created November 20, 2011 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buley/1381200 to your computer and use it in GitHub Desktop.
Save buley/1381200 to your computer and use it in GitHub Desktop.
Generalized cursor update
InDB.cursor.update = function (store, index, keyRange, data, direction, on_success, on_error, on_abort) {
var request, transaction, tx, transaction_index, update_request, cursor, value, attr;
try {
tx = InDB.db.transaction([database], type, timeout);
transaction = request.objectStore(database);
} catch (event) {
throw ('Transaction error');
};
if ('undefined' !== typeof index && null !== index) {
transaction_index = transaction.index(index);
request = transaction_index.openCursor(keyRange, direction);
} else {
request = transaction.openCursor(keyRange, direction);
}
request.onsuccess = function (open_event) {
cursor = open_event.target.result;
value = open_event.target.result.value;
for (attr in data) {
if (data.hasOwnProperty(attr)) {
value[attr] = data[attr];
}
}
if ("undefined" !== typeof cursor && null !== cursor && "undefined" !== typeof result && null !== result) {
update_request = cursor.update(data);
update_request.on_success = function (event) {
on_success(context);
if ('function' === typeof cursor.continue) {
cursor.continue();
}
}
}
}
request.onerror = function (open_event) {
on_error(open_event);
}
request.onabort = function (open_event) {
on_abort(open_event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment