Ibm mobilefirst 为什么无法将数据从适配器加载到JSONStore?
这是我在Ibm mobilefirst 为什么无法将数据从适配器加载到JSONStore?,ibm-mobilefirst,jsonstore,Ibm Mobilefirst,Jsonstore,这是我在main.js中的代码: 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
main.js
中的代码:
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;
}
这就是错误:
$('#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);
});
}
错误消息表示您传递的加载对象无效。这可能是因为您传递的是
param
,而不是params
。注意末尾的s
此外,该代码:
[wl.jsonstore] {"src":"load","err":18,"msg":"FAILED_TO_LOAD_INITIAL_DATA_FROM_ADAPTER_INVALID_LOAD_OBJ","col":"Contact","usr":"jsonstore","doc":{},"res":{}
看起来不对,也许你想写的是这样的:
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(集合)。然后(函数(){
WL.JSONStore.get(collectionName).count().then(函数(numberOfDocsInCollection){
如果(numberOfDocsInCollection<1){
WL.JSONStore.get(collectionName).load().then(函数(res){
//成功
})
}
})
});
为了简洁起见,我省略了处理故障。请注意,
load
将复制集合中已存在的项,因此检查集合是否为空的计数。这是错误:[wl.jsonstore]{“src”:“load”,“err”:18,“msg”:“未能从适配器加载初始数据\u无效\u load\u OBJ”,“col”:“Contact”,“usr”:“jsonstore”,“doc”:{},“res”:{}
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
})
}
})
});