Skip to content

Commit

Permalink
added phonegap contact methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil Maj committed Jan 22, 2012
1 parent dc50390 commit cb92a77
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions lib/plugin/Contact.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
var exec = require('phonegap/exec'),
ContactError = require('phonegap/plugin/ContactError'),
utils = require('phonegap/utils');

/**
* Contains information about a single contact.
* @constructor
Expand Down Expand Up @@ -35,4 +39,83 @@ var Contact = function (id, displayName, name, nickname, phoneNumbers, emails, a
this.urls = urls || []; // ContactField[]
};

/**
* Removes contact from device storage.
* @param successCB success callback
* @param errorCB error callback
*/
Contact.prototype.remove = function(successCB, errorCB) {
if (this.id === null) {
var errorObj = new ContactError(ContactError.UNKNOWN_ERROR);
errorCB(errorObj);
}
else {
exec(successCB, errorCB, "Contacts", "remove", [this.id]);
}
};

/**
* Creates a deep copy of this Contact.
* With the contact ID set to null.
* @return copy of this Contact
*/
Contact.prototype.clone = function() {
var clonedContact = utils.clone(this);
var i;
clonedContact.id = null;
clonedContact.rawId = null;
// Loop through and clear out any id's in phones, emails, etc.
if (clonedContact.phoneNumbers) {
for (i = 0; i < clonedContact.phoneNumbers.length; i++) {
clonedContact.phoneNumbers[i].id = null;
}
}
if (clonedContact.emails) {
for (i = 0; i < clonedContact.emails.length; i++) {
clonedContact.emails[i].id = null;
}
}
if (clonedContact.addresses) {
for (i = 0; i < clonedContact.addresses.length; i++) {
clonedContact.addresses[i].id = null;
}
}
if (clonedContact.ims) {
for (i = 0; i < clonedContact.ims.length; i++) {
clonedContact.ims[i].id = null;
}
}
if (clonedContact.organizations) {
for (i = 0; i < clonedContact.organizations.length; i++) {
clonedContact.organizations[i].id = null;
}
}
if (clonedContact.tags) {
for (i = 0; i < clonedContact.tags.length; i++) {
clonedContact.tags[i].id = null;
}
}
if (clonedContact.photos) {
for (i = 0; i < clonedContact.photos.length; i++) {
clonedContact.photos[i].id = null;
}
}
if (clonedContact.urls) {
for (i = 0; i < clonedContact.urls.length; i++) {
clonedContact.urls[i].id = null;
}
}
return clonedContact;
};

/**
* Persists contact to device storage.
* @param successCB success callback
* @param errorCB error callback
*/
Contact.prototype.save = function(successCB, errorCB) {
exec(successCB, errorCB, "Contacts", "save", [this]);
};


module.exports = Contact;

0 comments on commit cb92a77

Please sign in to comment.