为什么无法将数据从Adapter加载到JSONStore?

时间:2014-05-30 10:19:24

标签: ibm-mobilefirst jsonstore

function getListPhoneNumbers() {

    var data = {listContacts:[{name:'Ho Cong Vi',number:'12345666'},{name:'hcv',number:'6543218'}]};

    WL.Logger.info('Data:'+JSON.stringify(data));
    return data;
}

function addListPhoneNumber(data) {

    WL.Logger.debug('Add Data to JSONStore: ' + data);
    return;
}

function updateListPhoneNumber(data) {

    WL.Logger.debug('Updata Data from JSONStore: ' + data);
    return;

}


function deleteListPhoneNumber(data) {


    WL.Logger.debug('Delete Data from JSONStore: ' + data);
    return;

}

这是main.js中的代码:

$('#show-all-btn').on('click', showAllData);

var collectionName = 'Contacts',

collections = {};
collections[collectionName] = {
    searchFields: {
        name: 'string',
        number: 'string'
    },
    adapter: {
        name: 'listPhoneNumbers',
        add: 'addListPhoneNumber',
        replace: 'updateListPhoneNumber',
        remove: 'deleteListPhoneNumber',
        load: {
            procedure: 'getListPhoneNumbers',
            param: [],
            key: 'listContacts'
        }
    }
};

WL.JSONStore.init(collections)

    function showAllData() {

    $('#show-all-btn').on("click", function() {
        $('#info').show();
    });
    WL.JSONStore.get(collectionName).load().then(function(res) {
        alert('ok' + JSON.stringify(res));
    }).fail(function(errorObject) {
        alert(errorObject);
    });
}

这是错误:

[wl.jsonstore] {"src":"load","err":18,"msg":"FAILED_TO_LOAD_INITIAL_DATA_FROM_ADAPTER_INVALID_L‌​OAD_OBJ","col":"Contact","usr":"jsonstore","doc":{},"res":{}

1 个答案:

答案 0 :(得分:1)

错误消息表明您传递的加载对象无效。这可能是因为您通过了param而不是params。请注意最后的s

此外,此代码:

WL.JSONStore.init(collections)

    function showAllData() {

    $('#show-all-btn').on("click", function() {
        $('#info').show();
    });
    WL.JSONStore.get(collectionName).load().then(function(res) {
        alert('ok' + JSON.stringify(res));
    }).fail(function(errorObject) {
        alert(errorObject);
    });
}

看起来不对,也许你打算写的是这样的:

WL.JSONStore.init(collections).then(function () {

  WL.JSONStore.get(collectionName).count().then(function (numberOfDocsInCollection) {
    if(numberOfDocsInCollection < 1) {
      WL.JSONStore.get(collectionName).load().then(function(res) {
        //handle success
      })
    }
  })
});

为简洁起见,我省略了处理故障。请注意,load将复制集合中的项目(如果这些项目已存在),因此计算以检查集合是否为空。