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

[15/21] git commit: tweaks to contacts and tests

tweaks to contacts and tests


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/94b7fb0a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/94b7fb0a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/94b7fb0a

Branch: refs/heads/master
Commit: 94b7fb0aa61952b427504154f1ae30fbb1285ed7
Parents: 3360df0
Author: Fil Maj <ma...@gmail.com>
Authored: Wed Mar 14 15:17:08 2012 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Thu Mar 15 10:14:41 2012 -0700

----------------------------------------------------------------------
 lib/plugin/Contact.js |   97 +++++++++++++++++--------------------------
 test/test.contact.js  |   34 ++++++++--------
 2 files changed, 55 insertions(+), 76 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/94b7fb0a/lib/plugin/Contact.js
----------------------------------------------------------------------
diff --git a/lib/plugin/Contact.js b/lib/plugin/Contact.js
index c9b3de6..0c24e83 100644
--- a/lib/plugin/Contact.js
+++ b/lib/plugin/Contact.js
@@ -3,6 +3,23 @@ var exec = require('cordova/exec'),
     utils = require('cordova/utils');
 
 /**
+* Converts primitives into Complex Object
+* Currently only used for Date fields
+*/
+function convertIn(contact) {
+    var value = contact.birthday;
+    try {
+      contact.birthday = new Date(parseFloat(value));
+    } catch (exception){
+      console.log("Cordova Contact convertIn error: exception creating date.");
+    }
+    return contact;
+};
+
+
+	// convert birthday value to milliseconds - don't modify original(this) contact
+
+/**
 * Contains information about a single contact.
 * @constructor
 * @param {DOMString} id unique identifier
@@ -38,40 +55,6 @@ var Contact = function (id, displayName, name, nickname, phoneNumbers, emails, a
     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");
-	}            	    
-};
 
 /**
 * Removes contact from device storage.
@@ -79,12 +62,14 @@ Contact.prototype.convertIn = function() {
 * @param errorCB error callback
 */
 Contact.prototype.remove = function(successCB, errorCB) {
+    var fail = function(code) {
+        errorCB(new ContactError(code));
+    };
     if (this.id === null) {
-        var errorObj = new ContactError(ContactError.UNKNOWN_ERROR);
-        errorCB(errorObj);
+        fail(ContactError.UNKNOWN_ERROR);
     }
     else {
-    	exec(successCB, errorCB, "Contacts", "remove", [this.id]);
+        exec(successCB, fail, "Contacts", "remove", [this.id]);
     }
 };
 
@@ -148,29 +133,23 @@ Contact.prototype.clone = function() {
 * @param errorCB error callback
 */
 Contact.prototype.save = function(successCB, errorCB) {
+  var fail = function(code) {
+      errorCB(new ContactError(code));
+  };
 	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]);
+      if (result) {
+          if (typeof successCB === 'function') {
+              var fullContact = require('cordova/plugin/contacts').create(result);
+              successCB(convertIn(fullContact));
+          }
+      }
+      else {
+          // no Entry object returned
+          fail(ContactError.UNKNOWN_ERROR);
+      }
+  };
+	var dupContact = this.clone();
+	exec(success, fail, "Contacts", "save", [dupContact]);
 };
 
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/94b7fb0a/test/test.contact.js
----------------------------------------------------------------------
diff --git a/test/test.contact.js b/test/test.contact.js
index 3ece349..fe8919b 100644
--- a/test/test.contact.js
+++ b/test/test.contact.js
@@ -12,16 +12,16 @@ describe("Contact", function () {
             expect(c.displayName).toBe(null);
             expect(c.name).toBe(null);
             expect(c.nickname).toBe(null);
-            expect(c.phoneNumbers).toEqual([]);
-            expect(c.emails).toEqual([]);
-            expect(c.addresses).toEqual([]);
-            expect(c.ims).toEqual([]);
-            expect(c.organizations).toEqual([]);
+            expect(c.phoneNumbers).toEqual(null);
+            expect(c.emails).toEqual(null);
+            expect(c.addresses).toEqual(null);
+            expect(c.ims).toEqual(null);
+            expect(c.organizations).toEqual(null);
             expect(c.birthday).toBe(null);
             expect(c.note).toBe(null);
-            expect(c.photos).toEqual([]);
-            expect(c.categories).toEqual([]);
-            expect(c.urls).toEqual([]);
+            expect(c.photos).toEqual(null);
+            expect(c.categories).toEqual(null);
+            expect(c.urls).toEqual(null);
         });
 
         it("overrides default values with the arguments", function () {
@@ -67,14 +67,14 @@ describe("Contact", function () {
             c.id = 1;
             c.rawId = 1;
 
-            c.phoneNumbers.push({id: 1});
-            c.emails.push({id: 1});
-            c.addresses.push({id: 1});
-            c.ims.push({id: 1});
-            c.organizations.push({id: 1});
-            c.categories.push({id: 1});
-            c.photos.push({id: 1});
-            c.urls.push({id: 1});
+            c.phoneNumbers = [{id: 1}];
+            c.emails = [{id: 1}];
+            c.addresses = [{id: 1}];
+            c.ims = [{id: 1}];
+            c.organizations = [{id: 1}];
+            c.categories = [{id: 1}];
+            c.photos = [{id: 1}];
+            c.urls = [{id: 1}];
 
             var clone = c.clone();
 
@@ -98,7 +98,7 @@ describe("Contact", function () {
                 e = jasmine.createSpy();
 
             c.save(s, e);
-            expect(exec).toHaveBeenCalledWith(s, e, "Contacts", "save", [c]);
+            expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Contacts", "save", [c]);
         });
     });
 });