| var exec = require('cordova/exec'), |
| ContactError = require('cordova/plugin/ContactError'), |
| utils = require('cordova/utils'), |
| Contact = require('cordova/plugin/Contact'); |
| |
| /** |
| * Represents a group of Contacts. |
| * @constructor |
| */ |
| var contacts = { |
| /** |
| * Returns an array of Contacts matching the search criteria. |
| * @param fields that should be searched |
| * @param successCB success callback |
| * @param errorCB error callback |
| * @param {ContactFindOptions} options that can be applied to contact searching |
| * @return array of Contacts matching search criteria |
| */ |
| find:function(fields, successCB, errorCB, options) { |
| if (!successCB) { |
| throw new TypeError("You must specify a success callback for the find command."); |
| } |
| if (!fields || (utils.isArray(fields) && fields.length === 0)) { |
| if (typeof errorCB === "function") { |
| errorCB(new ContactError(ContactError.INVALID_ARGUMENT_ERROR)); |
| } |
| } else { |
| var win = function(result) { |
| var cs = []; |
| for (var i = 0, l = result.length; i < l; i++) { |
| cs.push(contacts.create(result[i])); |
| } |
| successCB(cs); |
| }; |
| exec(win, errorCB, "Contacts", "search", [fields, options]); |
| } |
| }, |
| |
| /** |
| * This function creates a new contact, but it does not persist the contact |
| * to device storage. To persist the contact to device storage, invoke |
| * contact.save(). |
| * @param properties an object who's properties will be examined to create a new Contact |
| * @returns new Contact object |
| */ |
| create:function(properties) { |
| var i; |
| var contact = new Contact(); |
| for (i in properties) { |
| if (typeof contact[i] !== 'undefined' && properties.hasOwnProperty(i)) { |
| contact[i] = properties[i]; |
| } |
| } |
| return contact; |
| } |
| }; |
| |
| module.exports = contacts; |