Building an Address Book > Creating the Application Functions > The doUpdateContact() Function

 

The doUpdateContact() Function

The doUpdateContact() function shares much in common with the doAddContact() function. It saves some contact data, returns the address book to "browse" mode, and refreshes the on-screen contact information. Like doAddContact(), doUpdateContact() is a callback for the "Done" button's click handler.

The doUpdateContact() function performs the following tasks:

Disables the "contact details" pane and enables the "address book" pane.

Updates an existing contact in the local SharedObject.

Refills the contactListBox_lb with the latest contact list.

Selects the updated contact in contactListBox_lb.

Places the updated contact information in the "contact details" pane.

The complete listing for doUpdateContact() is shown below. The comments explain the minor differences from code in doAddContact().

/*
 * Function: doUpdateContact()
 *   Desc: Revises an existing contact in the local shared object and
 *         returns the app to regular "browse" mode.
 *   Parameters: None.
 */

function doUpdateContact () 
{
  // Return to the normal address book mode. Same as doAddContact().
  exitEditMode();

  // Because we're updating, not adding, we need the ID of
  // the contact to change. We get the ID from the data value 
  // of the selected ListBox item.
  var selectedID = contactListBox_lb.getValue();

  // When the update is complete, we want to re-select the
  // original selection in contactListBox_lb (the selection 
  // will be lost when we populate the ListBox). Hence we must
  // make note of the ListBox's selected item before repopulating.
  var oldSelectedIndex = contactListBox_lb.getSelectedIndex();

  // Now we use updateContact() to revise the the contact data.
  // Just as with addContact(), we grab the information from the
  // text fields and ComboBox in the "contact details" pane.
  contactMgr.updateContact(selectedID, 
                           firstNameField.text,
                           lastNameField.text,
                           emailField.text,
                           phoneField.text,
                           phoneComboBox_cb.getSelectedIndex(),
                           companyField.text,
                           commentsField.text);

  // Now we refill the entries in the address book. Our custom
  // populateListBox() function takes the desired new selected
  // index as a parameter.
  populateListBox(oldSelectedIndex);
}