获取用户的联系人列表并使用 Onsen UI 模板将其连接到后端数据库

Get user's contact list and connect it to database on backendless using Onsen UI template

尝试使用 "Hello world" Onsen template 但使用 GetContacts 函数我得到的只是 "number of contacts" 和 "third contact name"。

如何检索整个联系人列表?

该模板简单地演示了如何使用 cordova api。如果您查看您提到的函数的实现,您会看到如下内容:

function getContacts() {
  var options = new ContactFindOptions();
  options.filter = "";
  options.multiple = true;
  navigator.contacts.find(["displayName", "name"], success, fail, options);
}

function success(contacts) { 
  alert(contacts.length + ' contacts returned.' + (contacts[2] && contacts[2].name ? (' Third contact is ' + contacts[2].name.formatted) : ''));
}

所以在你的情况下你可以做同样的事情 - 除了只显示第二个联系人你还可以对所有联系人做一些事情:

function success(contacts) { 
  for (var i = 0; i < contacts.length; i++) {
    console.log('contacts[' + i + '] = ' + contacts[i].name && contacts[i].name.formatted);
  }
}

您可能还对 navigator.contacts.finddocumentation 感兴趣。

至于将信息与backendless连接起来——你只是在标题中提到了它,并没有正确解释你想做什么。另外,检索数据和连接数据对我来说听起来像是两个不同的问题。为了连接它,您可以提出一个单独的问题,您可以在其中提供有关要对数据执行的操作的更多信息,并可能显示一些无后端代码(如果有的话)。

function contacts_success(contacts) {
    for (var i = 0; i < contacts.length; i++) {
        console.log(
          contacts.length + ' contacts returned. ' +
          (contacts[i] && contacts[i].name ? ('All contacts are ' + contacts[i].name.formatted) : '')
        );
    }
}

function contacts_failed(msgObject) {
    alert("Failed to access contact list:" + JSON.stringify(msgObject));
}

function get_contacts() {
    var obj = new ContactFindOptions();
    obj.filter = "";
    obj.multiple = true;
    navigator.contacts.find([ "displayName", "name" ], contacts_success, contacts_failed, obj);
}

最终代码:这就像一个魅力,让我用逗号分隔每个字段。非常感谢 Ilia Yatchev!现在只需要找到将这些 console.log 数据上传到后端数据库的解决方案。

function contacts_success(contacts) {
    for (var i = 0; i < contacts.length; i++) {
        console.log(contacts[i]);
    }
}

function contacts_failed(msgObject){
    alert("Failed to access contact list:" + JSON.stringify(msgObject));
}

function get_contacts() {
    var obj = new ContactFindOptions();
    obj.filter = "";
    obj.multiple = true;
    navigator.contacts.find(
            [ "displayName", "name", "phoneNumbers" ], contacts_success,
            contacts_failed, obj);
}

无后端代码:

Backendless.serverURL = "https://api.backendless.com";

Backendless.initApp(APPLICATION_ID, SECRET_KEY, VERSION);

var $rootScope = window;


function cleanPrivateRelations(data) {
    function isObject(obj) {
        return obj !== null && typeof obj === 'object';
    }

    if (data.hasOwnProperty('_private_relations') &&    
      data['_private_relations'].length > 0) {
        data['_private_relations'].forEach(function(relation) {
            if (data.hasOwnProperty(relation) && isObject(data[relation])) {
                if (Array.isArray(data[relation])) {
                    data[relation].forEach(function(elem) {
                        if (isObject(elem)) {
                            cleanPrivateRelations(elem);
                        }
                    });
                } else {
                    cleanPrivateRelations(data[relation]);
                }
            }
        });
    }

    if (isObject(data)) {
        delete data['_private_relations'];
        delete data['_private_geoRelations'];
        delete data['_private_dates'];
    }
}

$rootScope.Classes = {
    contacts: function contacts( args ) {
        args = args || {};

        this.objectId = args.objectId || null;
        this.updated = args.updated || null;
        this.contacts = args.contacts || null;
        this.ownerId = args.ownerId || null;
        this.created = args.created || null;

        this._private_relations = [];
        this._private_geoRelations = [];
        this._private_dates = ["updated", "created"];
        this.___class = "contacts";

        var storage = Backendless.Persistence.of( contacts );

        this.save = function ( async ) {
            cleanPrivateRelations(this);
            storage.save( this, async );
        };

        this.remove = function ( async ) {
            var result = storage.remove( this, async );
            this.objectId = null;
            return result;
        };

        this._private_describeClass = function() {
            return Backendless.Persistence.describe(this.___class);
        };
    }
}