Advertisement
Guest User

Cordova Contact Plugin Bug - Can't remove contact field

a guest
Mar 28th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. document.addEventListener("deviceready", onDeviceReady, false);
  2.  
  3.   function onDeviceReady() {
  4.     console.log("LETS GO");
  5.     var myContact = navigator.contacts.create({"displayName": "Test User"});
  6.  
  7.     var phoneNumbers = [];
  8.  
  9.     phoneNumbers[0] = new ContactField('work', '212-555-1234', false);
  10.     phoneNumbers[1] = new ContactField('mobile', '917-555-5432', true); // preferred number
  11.     phoneNumbers[2] = new ContactField('home', '203-555-7890', false);
  12.  
  13.  
  14.     myContact.phoneNumbers = phoneNumbers;
  15.  
  16.     console.log("Contact to be saved", phoneNumbers);
  17.  
  18.     myContact.save(function () {
  19.       // Success
  20.       updateContact(function () {
  21.         // Callback after the Test User has been updated
  22.         getTestUserContact(function (testContact) {
  23.           console.log("The contact in the system looks now like this:", testContact);
  24.           // Tidy Up
  25.           testContact.remove();
  26.         });
  27.  
  28.  
  29.  
  30.       });
  31.  
  32.     }, function () {
  33.       // error
  34.  
  35.       console.error("Well, something went wrong...", err);
  36.  
  37.     });
  38.  
  39.   }
  40.  
  41.  
  42.   function updateContact(callback) {
  43.     navigator.contacts.find(["*"], function (contacts) {
  44.  
  45.       // Success
  46.  
  47.       console.log("BEFORE UPDATE", contacts);
  48.  
  49.       var index = 0;
  50.  
  51.       // Find the Test User, we previously created
  52.       while (index != contacts.length) {
  53.         var contact = contacts[index];
  54.  
  55.         if (contact.nickname == "Test User") { // FIXME: I dont have a clue why the displayName attribute is saved to the nickname attribute....
  56.           // Remove the last phone number from the array
  57.           delete contact.phoneNumbers[2];
  58.  
  59.           console.log("Contact after removing a phoneNumber", contact);
  60.  
  61.           // Save the Test User contact
  62.           contact.save();
  63.  
  64.           callback();
  65.  
  66.           return;
  67.         }
  68.  
  69.         index++;
  70.       }
  71.  
  72.       console.error("Could not find Test User.");
  73.  
  74.  
  75.     }, function (err) {
  76.  
  77.       // Error
  78.       console.error("Well, something went wrong...", err);
  79.  
  80.     });
  81.   }
  82.  
  83.   function getTestUserContact(callback) {
  84.     navigator.contacts.find(["*"], function (contacts) {
  85.  
  86.       // Success
  87.  
  88.       var index = 0;
  89.  
  90.       // Find the Test User, we previously created
  91.       while (index != contacts.length) {
  92.         var contact = contacts[index];
  93.  
  94.         if (contact.nickname == "Test User") {
  95.  
  96.           callback(contact);
  97.           return;
  98.         }
  99.  
  100.         index++;
  101.       }
  102.  
  103.       console.error("Could not find Test User.");
  104.     });
  105.  
  106.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement