You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by he...@apache.org on 2014/01/09 21:10:38 UTC

[03/21] git commit: save is working

save is working


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/commit/fb78d22b
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/fb78d22b
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/fb78d22b

Branch: refs/heads/master
Commit: fb78d22bb0485c091fa906e9d820bab57c9f0bbb
Parents: 1dbd371
Author: Piotr Zalewa <pi...@zalewa.info>
Authored: Wed Nov 13 19:28:47 2013 +0100
Committer: Piotr Zalewa <pi...@zalewa.info>
Committed: Wed Nov 13 19:28:47 2013 +0100

----------------------------------------------------------------------
 src/firefoxos/ContactsProxy.js | 61 +++++++++++++++++++++----------------
 1 file changed, 34 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/fb78d22b/src/firefoxos/ContactsProxy.js
----------------------------------------------------------------------
diff --git a/src/firefoxos/ContactsProxy.js b/src/firefoxos/ContactsProxy.js
index 7029541..c2ac0fd 100644
--- a/src/firefoxos/ContactsProxy.js
+++ b/src/firefoxos/ContactsProxy.js
@@ -27,10 +27,13 @@
 // https://developer.mozilla.org/en-US/docs/Web/API/mozContact
 function saveContacts(successCB, errorCB, contacts) {
     // success and fail will be called every time a contact is saved
-    function success(result) {
-        // TODO: this will need to amend the result
-        console.log('SUCCESS from moz');
-        successCB(result);
+    // a closure which is holding the object to be returned to sucessCB
+    function makeSuccess(contact, moz) {
+        return function(result) {
+            // TODO modify contact so it will contain the link to moz
+            // call callback
+            successCB(contact);
+        }
     }
     function error(e) {
         console.log('BOO from moz');
@@ -75,36 +78,42 @@ function saveContacts(successCB, errorCB, contacts) {
         var request;
         // prepare mozContact object
         var translatedContact = {};
+        // building name
         var nameArray = [];
-        var j = 0;
-        var field;
         var fields = ['honorificPrefix', 'familyName', 'givenName', 'middleName', 'nickname'];
-        while(field = fields[j++]) {
+        var j = 0, field; while(field = fields[j++]) {
             if (contact.name[field]) {
                 nameArray.push(contact.name[field]);
             }
         }
         translatedContact.name = nameArray.join(' ');
-        if (contact.name.honorificPrefix) {
-            translatedContact.honorificPrefix = contact.name.honorificPrefix;
+        // adding simple fields [contactField, eventualMozContactField]
+        var simpleFields = [['honorificPrefix'], ['givenName'], ['familyName'], 
+            ['honorificSuffix'], ['nickname'], ['birthday', 'bday'], ['note']];
+        j = 0; while(field = simpleFields[j++]) {
+          if (contact.name[field[0]]) {
+            translatedContact[field[1] || field[0]] = contact.name[field[0]];
+          }
+        }
+        if (contact.emails) {
+            translatedContact.email = exportContactFieldArray(contact.emails);
+        }
+        if (contact.categories) {
+            translatedContact.category = exportContactFieldArray(contact.categories);
+        }
+        if (contact.addresses) {
+            translatedContact.adr = exportAddress(contact.addresses);
         }
-        translatedContact.familyName = contact.name.familyName;
-        /*
-            honorificPrefix: [contact.name.honorificPrefix],
-            givenName: [contact.name.givenName],
-            familyName: [contact.name.familyName],
-            honorificSuffix: [contact.name.honorificSuffix], 
-            nickname: [contact.nickname],
-            email: exportContactFieldArray(contact.emails),
+        if (contact.phoneNumbers) {
+            translatedContact.tel = exportContactFieldArray(contact.phoneNumbers);
+        }
+        if (contact.organizations) {
+            translatedContact.org = exportContactFieldArray(contact.organizations, 'name');
+            translatedContact.jobTitle = exportContactFieldArray(contact.organizations, 'title');
+        }
+        /* 
             // photo: Blob
             // url: Array with metadata (?)
-            category: exportContactFieldArray(contact.categories),
-            adr: exportAddress(contact.addresses),
-            tel: exportContactFieldArray(contact.phoneNumbers),
-            org: exportContactFieldArray(contact.organizations, 'name'),
-            jobTitle: exportContactFieldArray(contact.organizations, 'title'),
-            bday: contact.birthday,
-            note: contact.note,
             // impp: exportIM(contact.ims), TODO: find the moz impp definition
             // anniversary
             // sex
@@ -114,10 +123,8 @@ function saveContacts(successCB, errorCB, contacts) {
         */
         // TODO: find a way to link existing mozContact and Contact by ID
         var moz = new mozContact(translatedContact);
-        // XXX: moz.name is undefined
-        
         request = navigator.mozContacts.save(moz);
-        request.onsuccess = success;
+        request.onsuccess = makeSuccess(contact, moz);
         request.onerror = error;                
     }
 }