Created
August 30, 2013 19:45
-
-
Save tkh44/6393600 to your computer and use it in GitHub Desktop.
Local PhoneGap contacts to Backbone collection
This file contains hidden or 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
| (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