Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- document.addEventListener("deviceready", onDeviceReady, false);
- function onDeviceReady() {
- console.log("LETS GO");
- var myContact = navigator.contacts.create({"displayName": "Test User"});
- var phoneNumbers = [];
- phoneNumbers[0] = new ContactField('work', '212-555-1234', false);
- phoneNumbers[1] = new ContactField('mobile', '917-555-5432', true); // preferred number
- phoneNumbers[2] = new ContactField('home', '203-555-7890', false);
- myContact.phoneNumbers = phoneNumbers;
- console.log("Contact to be saved", phoneNumbers);
- myContact.save(function () {
- // Success
- updateContact(function () {
- // Callback after the Test User has been updated
- getTestUserContact(function (testContact) {
- console.log("The contact in the system looks now like this:", testContact);
- // Tidy Up
- testContact.remove();
- });
- });
- }, function () {
- // error
- console.error("Well, something went wrong...", err);
- });
- }
- function updateContact(callback) {
- navigator.contacts.find(["*"], function (contacts) {
- // Success
- console.log("BEFORE UPDATE", contacts);
- var index = 0;
- // Find the Test User, we previously created
- while (index != contacts.length) {
- var contact = contacts[index];
- if (contact.nickname == "Test User") { // FIXME: I dont have a clue why the displayName attribute is saved to the nickname attribute....
- // Remove the last phone number from the array
- delete contact.phoneNumbers[2];
- console.log("Contact after removing a phoneNumber", contact);
- // Save the Test User contact
- contact.save();
- callback();
- return;
- }
- index++;
- }
- console.error("Could not find Test User.");
- }, function (err) {
- // Error
- console.error("Well, something went wrong...", err);
- });
- }
- function getTestUserContact(callback) {
- navigator.contacts.find(["*"], function (contacts) {
- // Success
- var index = 0;
- // Find the Test User, we previously created
- while (index != contacts.length) {
- var contact = contacts[index];
- if (contact.nickname == "Test User") {
- callback(contact);
- return;
- }
- index++;
- }
- console.error("Could not find Test User.");
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement