Newer Version Available
Updating Records with Remote Objects
Update records by calling update() on a Remote Objects model instance.
update() accepts three
arguments, all optional, and can update one or many records at the
same time, depending on the arguments that you provide.
1RemoteObjectModel.update([record_ids], {field_values}, 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 update a record
is to call update() on itself.
1ctDetails = {FirstName: "Marc", LastName: "Benioff"};
2ct = new RemoteObjectModel.Contact(ctDetails);
3ct.create();
4
5// Later, in response to a page event...
6ct.set('Phone', '555-1212');
7ct.update();More often, you might need to update a record in response to a
form submission. Updating the record can be as simple as reading some
form values, including the record’s Id, and passing the values to update(). For example:
1var record = new RemoteObjectModel.Contact();
2record.update({
3 Id: $j('#contactId').val(),
4 FirstName: $j('#fName').val(),
5 LastName: $j('#lName').val(),
6 Phone: $j('#phone').val(),
7 Notes: $j('#notes').val()
8});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.
1// Handle the Save button
2function updateContact(e){
3 e.preventDefault();
4
5 var record = new RemoteObjectModel.Contact({
6 Id: $jQuery('#contactId').val(),
7 FirstName: $jQuery('#fName').val(),
8 LastName: $jQuery('#lName').val(),
9 Phone: $jQuery('#phone').val(),
10 Notes: $jQuery('#notes').val()
11 });
12 record.update(updateCallback);
13}
14
15// Callback to handle DML Remote Objects calls
16function updateCallback(err, ids){
17 if (err) {
18 displayError(err);
19 } else {
20 // Reload the contacts with current list
21 getAllContacts();
22 $jQuery.mobile.changePage('#listpage', {changeHash: true});
23 }
24}You can update many records at the same time, as long as the update
to be performed is uniform, that is, the same for every record. For
example, you might need to update a collection of checked items from
a list, to change a status field to “Archived” or a
current timestamp. To update records in one request, pass an array
of Ids to update(). The fields to be updated
can be set as part of the Remote Object model itself, but it’s
safer to pass them directly to update(), like this:
1var ct = new RemoteObjectModel.Contact();
2ct.update(
3 ['003xxxxxxxxxxxxxxx', '003xxxxxxxxxxxxxxx'],
4 { FirstName: "George", LastName: "Foreman" },
5 function(err, ids) {
6 if (err) {
7 displayError(err);
8 } else {
9 // Reload the contacts with current list
10 getAllContacts();
11 $jQuery('#status').html(ids.length + ' record(s) updated.');
12 $jQuery.mobile.changePage('#listpage', {changeHash: true});
13 }
14});