Deleting Records with Remote Objects
Delete records by calling del() on a Remote
Objects model instance.
del() accepts two arguments,
both optional, and can delete one or many records, depending on the
arguments that you provide.
RemoteObjectModel.del([record_ids], callback_function)
record_ids is an array of strings, where the strings are
the Ids of records to be deleted. If this
parameter is omitted, the Id that is set on the
Remote Object instance is used. The simplest way to delete a record is to call del() on
itself.
ctDetails = {FirstName: "Tobe", LastName: "Ornottobe"};
ct = new RemoteObjectModel.Contact(ctDetails);
ct.create();
// After some thought, and the async operation completes...
// It's not to be; delete the contact
ct.del();
More often, you might need to delete a record in response to a
button click. Deleting the record is as simple as getting the record’s Id from the page and then passing
the Id to del(). For example:
var id = $jQuery('#contactId').val();
var ct = new RemoteObjectModel.Contact();
ct.del(id);
Robust code includes a callback to handle errors. The following
code accomplishes the same as the previous sample, altered to use
an event handler and a callback function.
// Handle the delete button click
function deleteContact(e){
e.preventDefault();
var ct = new RemoteObjectModel.Contact();
ct.del($jQuery('#contactId').val(), updateCallback);
}
// Callback to handle DML Remote Objects calls
function updateCallback(err, ids){
if (err) {
displayError(err);
} else {
// Reload the contacts with current list
getAllContacts();
$jQuery.mobile.changePage('#listpage', {changeHash: true});
}
}
To delete multiple records in one request—for example, checked
items from a list—pass an array of Ids to del().
var ct = new RemoteObjectModel.Contact();
ct.del(['003xxxxxxxxxxxxxxx', '003xxxxxxxxxxxxxxx'], function(err, ids) {
if (err) {
displayError(err);
} else {
// Reload the contacts with current list
getAllContacts();
$jQuery('#status').html(ids.length + ' record(s) deleted.');
$jQuery.mobile.changePage('#listpage', {changeHash: true});
}
});