search if contact does exist before remove
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/b52fafd2 Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/b52fafd2 Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/b52fafd2 Branch: refs/heads/master Commit: b52fafd2465a1b175fe00b4f3caa2ffc88898791 Parents: 510955f Author: Piotr Zalewa <[email protected]> Authored: Wed Dec 18 09:45:31 2013 +0100 Committer: Piotr Zalewa <[email protected]> Committed: Wed Dec 18 09:45:31 2013 +0100 ---------------------------------------------------------------------- src/firefoxos/ContactsProxy.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/b52fafd2/src/firefoxos/ContactsProxy.js ---------------------------------------------------------------------- diff --git a/src/firefoxos/ContactsProxy.js b/src/firefoxos/ContactsProxy.js index 13a3486..4d99428 100644 --- a/src/firefoxos/ContactsProxy.js +++ b/src/firefoxos/ContactsProxy.js @@ -252,13 +252,24 @@ function remove(successCB, errorCB, ids) { for (var i=0; i < ids.length; i++){ // throw an error if no id provided if (!_hasId(ids[i])) { - errorCB(0); + console.error('FFOS: Attempt to remove unsaved contact'); + errorCB(0); + return; } - var moz = new mozContact(); - moz.id = ids[i]; - var request = navigator.mozContacts.remove(moz); - request.onsuccess = successCB; - request.onerror = errorCB; + var search = navigator.mozContacts.find({ + filterBy: ['id'], filterValue: ids[i], filterOp: 'equals'}); + search.onsuccess = function() { + if (search.result.length === 0) { + console.error('FFOS: Attempt to remove a non existing contact'); + errorCB(0); + return; + } + var moz = search.result[0]; + var request = navigator.mozContacts.remove(moz); + request.onsuccess = successCB; + request.onerror = errorCB; + }; + search.onerror = errorCB; } }
