February 18, 2012

matsimoto matsimoto
Lab Rat
18 posts

[Solved] How to add a phone number to an existing contact with javascript ?

 

Hi,

I wonder how I can add a phone number to an existing contact ? Are there any Code snippets available ?

Thanks.

Bye
Matsimoto

4 replies

February 18, 2012

task_struct task_struct
Ant Farmer
349 posts

Hi,

I wrote this using this example [doc.qt.nokia.com]

  1. Rectangle {
  2.      id: topItem
  3.      width: 360
  4.      height: 640
  5.      x: 0
  6.      y: 0
  7.  
  8.      Contact {
  9.          id: myContact
  10.          Name {
  11.              firstName:"John"
  12.              lastName:"Gates"
  13.          }
  14.  
  15.          EmailAddress {
  16.              emailAddress:"john@example.com"
  17.          }
  18.          EmailAddress {
  19.              emailAddress:"mygmailaccount@gmail.com"
  20.          }
  21.  
  22.          address.street:"53 Mysteet St"
  23.          address.locality: "My City"
  24.          address.region: "My Region"
  25.          address.postcode:"1111"
  26.          address.country:"My Country"
  27.          address.subTypes:[Address.Postal, Address.Domestic]
  28.          address.postOfficeBox:"1111"
  29.  
  30.          Nickname {
  31.              nickname:"John"
  32.          }
  33.  
  34.          PhoneNumber {
  35.              number: "1111111111"
  36.              subTypes:[PhoneNumber.Mobile]
  37.          }
  38.  
  39.          PhoneNumber {
  40.              number: "2222222222"
  41.              subTypes:[PhoneNumber.Fax]
  42.          }
  43.  
  44.          PhoneNumber {
  45.              number: "3333333333"
  46.              subTypes:[PhoneNumber.Landline]
  47.          }
  48.  
  49.          Component.onCompleted: {
  50.              // create new phone number
  51.              var newNumber = Qt.createQmlObject('import QtMobility.contacts 1.1; PhoneNumber { number: "1234567890" }',
  52.                  myContact, "newNumber");
  53.  
  54.              myContact.addDetail( newNumber )
  55.              myContact.save();
  56.  
  57.              for( var i = 0; i < myContact.phoneNumbers.length; ++i ) {
  58.                  console.debug( "Phone number: " + myContact.phoneNumbers[i].number )
  59.              }
  60.          }
  61.      }
  62.  
  63.  
  64.      Column {
  65.          spacing:4
  66.  
  67.          //access the same property with different syntaxes
  68.          Text { text:"Name(from property name):" + myContact.name.firstName + " " + myContact.name.lastName }
  69.          Text { text:"Name(from detail type):" + myContact.detail(ContactDetail.Name).firstName + " " + myContact.name.lastName }
  70.          Text { text:"Name(from detail name):" + myContact.detail("Name").firstName + " " + myContact.name.lastName }
  71.  
  72.          Text { text:"Address:" + myContact.address.street + " " + myContact.address.locality + " " + myContact.address.region + " " + myContact.address.postcode }
  73.  
  74.          //If a contact contains multiple details for the same detail type, you can access them with the dynamic property names.
  75.          Text { text:"How many email accounts?:" + myContact.emails.length }
  76.          Text { text:"Email[0]:" + myContact.emails[0].emailAddress }
  77.  
  78.          Text { text:"How many phone numbers?:" + myContact.phoneNumbers.length }
  79.          Text { text:"phone number[0]:" + myContact.phoneNumbers[0].number }
  80.          Text { text:"phone number[1]:" + myContact.phoneNumbers[1].number }
  81.          Text { text:"phone number[2]:" + myContact.phoneNumbers[2].number }
  82.      }
  83.  }

See Component.onCompleted . It should work everywhere you can access contancts from ContactManager.
I hope this will be usefull for you :)

 Signature 

“Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.”
- Linus Torvalds

February 18, 2012

matsimoto matsimoto
Lab Rat
18 posts

Hi,

OK thanks a lot.
Your post clarified many other things too.

Ciao Matsimoto

February 18, 2012

task_struct task_struct
Ant Farmer
349 posts

Nice to hear this :)

May be I should add this code as snippet in the Wiki.

 Signature 

“Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.”
- Linus Torvalds

February 18, 2012

matsimoto matsimoto
Lab Rat
18 posts

Jepp, I think the main thing that a someone new to QML/Javascript/QT is that this Qt.createQmlObject command is the way to create the QML Objects needed.

 
  ‹‹ FolderListModel custom property      change display properties ››

You must log in to post a reply. Not a member yet? Register here!