You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2012/03/15 20:36:25 UTC

[20/21] git commit: Updated for iOS Contacts

Updated for iOS Contacts

*Need to create a complete Contact object on return from save.
*Need to convert from JS Dates to milliseconds and vice versa when
roundtripping to device.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/3360df0e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/3360df0e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/3360df0e

Branch: refs/heads/master
Commit: 3360df0ea07ff42321c516ab5e521a287d4a74a2
Parents: 9011d1e
Author: Becky Gibson <be...@apache.org>
Authored: Tue Mar 13 13:40:23 2012 -0400
Committer: Fil Maj <ma...@gmail.com>
Committed: Thu Mar 15 10:14:41 2012 -0700

----------------------------------------------------------------------
 lib/exec/ios.js       |    4 +-
 lib/plugin/Contact.js |   76 ++++++++++++++++++++++++++++++++++++++------
 2 files changed, 68 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/3360df0e/lib/exec/ios.js
----------------------------------------------------------------------
diff --git a/lib/exec/ios.js b/lib/exec/ios.js
index e97252c..32d8b6a 100644
--- a/lib/exec/ios.js
+++ b/lib/exec/ios.js
@@ -67,8 +67,8 @@ module.exports = function() {
     for (var i = 0; i < actionArgs.length; ++i) {
         var arg = actionArgs[i];
         if (arg == undefined || arg == null) { // nulls are pushed to the args now (becomes NSNull)
-            command.arguments.push(arg);
-        } else if (typeof(arg) == 'object') {
+            continue;  //command.arguments.push(arg);
+        } else if (typeof(arg) == 'object' && !(arg instanceof Array)) {
             command.options = arg;
         } else {
             command.arguments.push(arg);

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/3360df0e/lib/plugin/Contact.js
----------------------------------------------------------------------
diff --git a/lib/plugin/Contact.js b/lib/plugin/Contact.js
index 1aa70d4..c9b3de6 100644
--- a/lib/plugin/Contact.js
+++ b/lib/plugin/Contact.js
@@ -27,16 +27,50 @@ var Contact = function (id, displayName, name, nickname, phoneNumbers, emails, a
     this.displayName = displayName || null;
     this.name = name || null; // ContactName
     this.nickname = nickname || null;
-    this.phoneNumbers = phoneNumbers || []; // ContactField[]
-    this.emails = emails || []; // ContactField[]
-    this.addresses = addresses || []; // ContactAddress[]
-    this.ims = ims || []; // ContactField[]
-    this.organizations = organizations || []; // ContactOrganization[]
+    this.phoneNumbers = phoneNumbers || null; // ContactField[]
+    this.emails = emails || null; // ContactField[]
+    this.addresses = addresses || null; // ContactAddress[]
+    this.ims = ims || null; // ContactField[]
+    this.organizations = organizations || null; // ContactOrganization[]
     this.birthday = birthday || null;
     this.note = note || null;
-    this.photos = photos || []; // ContactField[]
-    this.categories = categories || []; // ContactField[]
-    this.urls = urls || []; // ContactField[]
+    this.photos = photos || null; // ContactField[]
+    this.categories = categories || null; // ContactField[]
+    this.urls = urls || null; // ContactField[]
+};
+/** 
+*	Converts Complex objects into primitives
+*   Only conversion at present is for Dates.
+**/
+Contact.prototype.convertOut = function() {
+	var value = this.birthday;
+    if (value != null) {
+    	// try to make it a Date object if it is not already
+    	if (!value instanceof Date){
+			try {
+				value = new Date(value);
+			} catch(exception){
+				value = null;
+			}
+		}
+		if (value instanceof Date){
+			value = value.valueOf(); // convert to milliseconds
+		}
+		this.birthday = value;
+    }
+};
+
+/**
+* Converts primitives into Complex Object
+* Currently only used for Date fields
+*/
+Contact.prototype.convertIn = function() {
+	var value = this.birthday;
+	try {
+		this.birthday = new Date(parseFloat(value));
+	} catch (exception){
+		console.log("exception creating date");
+	}            	    
 };
 
 /**
@@ -50,7 +84,7 @@ Contact.prototype.remove = function(successCB, errorCB) {
         errorCB(errorObj);
     }
     else {
-        exec(successCB, errorCB, "Contacts", "remove", [this.id]);
+    	exec(successCB, errorCB, "Contacts", "remove", [this.id]);
     }
 };
 
@@ -114,7 +148,29 @@ Contact.prototype.clone = function() {
 * @param errorCB error callback
 */
 Contact.prototype.save = function(successCB, errorCB) {
-    exec(successCB, errorCB, "Contacts", "save", [this]);
+	var success = function(result) {
+            if (result) {
+                if (typeof successCB === 'function') {
+                    var fullContact = require('cordova/plugin/contacts').create(result);
+                    fullContact.convertIn();
+            	    try {
+                        successCB(fullContact);
+                    }
+                    catch (e) {
+                        console.log('Error invoking callback: ' + e);
+                    }
+                }
+            }
+            else {
+                // no Entry object returned
+                errorCB(ContactError.UNKNOWN_ERROR);
+            }
+    };
+	// convert birthday value to milliseconds - don't modify original(this) contact
+	var dupContact = utils.clone(this);
+	dupContact.convertOut(); 
+	
+	exec(success, errorCB, "Contacts", "save", [dupContact]);
 };