Skip to content

Instantly share code, notes, and snippets.

@timdream
Created June 10, 2012 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timdream/2905079 to your computer and use it in GitHub Desktop.
Save timdream/2905079 to your computer and use it in GitHub Desktop.
/* 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