Skip to content

Instantly share code, notes, and snippets.

@tkh44
Created August 30, 2013 19:45
Show Gist options
  • Select an option

  • Save tkh44/6393600 to your computer and use it in GitHub Desktop.

Select an option

Save tkh44/6393600 to your computer and use it in GitHub Desktop.
Local PhoneGap contacts to Backbone collection
(function() {
// Attach to namespaced window object
window.ioffice = window.ioffice || {};
ioffice.collections = ioffice.collections || {};
ioffice.collections.contacts = Backbone.Collection.extend({
model: Backbone.Model,
getContacts: function(filter, cb) {
if (typeof ContactFindOptions === "undefined") {
if (cb) cb();
return;
}
var fields = ['name', 'displayName', 'emails'],
options = new ContactFindOptions(),
callback = cb || $.noop,
self = this;
options.filter = filter || '';
options.multiple = true;
this.reset();
var onSuccess = function(contacts) {
var self = this;
_.each(contacts, function(contact) {
var name = contact.name,
email;
if (contact.emails) {
email = _.findWhere(contact.emails, {'type': 'work'}) || contact.emails[0];
}
if (email) {
self.add({
firstName: name.givenName || '',
lastName: name.familyName || '',
email: email.value,
local: true
})
}
});
callback();
};
var onError = function(error) {
console.log(error);
callback();
};
navigator.contacts.find(fields, _.bind(onSuccess, this), _.bind(onError, this), options);
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment