public
Created

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/* 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);
};
}
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.