Created
June 10, 2012 11:41
-
-
Save timdream/2905079 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Contact Manager for maintaining contact cache and access contact db. | |
* 1. Maintain used contacts in contactData object literal. | |
* 2. getContactData: Call the callback with contact data. | |
* Callback will be called twice if cached data turned out to be different than | |
* the data from db. | |
*/ | |
var ContactDataManager = { | |
contactData: {}, | |
getContactData: function cm_getContactData(options, callback) { | |
var isCacheable = (options.filterBy.indexOf('tel') !== -1 && | |
options.filterOp == 'contains'); | |
// Fire callback first with cache data if data exists | |
if (isCacheable && | |
typeof this.contactData[options.filterValue] !== 'undefined') | |
callback(this.contactData[options.filterValue]); | |
var self = this; | |
var req = window.navigator.mozContacts.find(options); | |
req.onsuccess = function onsuccess() { | |
var result = req.result; | |
if (isCacheable) { | |
if (this.contactData[options.filterValue] !== result) | |
callback(result); | |
this.contactData[options.filterValue] = result; | |
return; | |
} | |
callback(result); | |
}; | |
req.onerror = function onerror() { | |
var msg = 'Contact finding error. Error: ' + req.errorCode; | |
console.log(msg); | |
callback(null); | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment