【发布时间】:2015-12-04 02:40:01
【问题描述】:
我一直在测试和学习将 JSONStore 与 MobileFirst SQL 适配器结合使用。一切正常,但我找不到下一个障碍的解决方案。
当我通过替换编辑 JSONStore 集合中的项目时,它在本地运行良好。当我推送集合时,它会正确到达适配器。问题是我无法告诉数据库要更新哪条记录。这样,它只会将所有记录更新为给定的信息。
被初始化的集合:
caller.open = function(){
WL.JSONStore.destroy();
var collections = {
people: {
searchFields: {
name: 'string',
age: 'integer'},
//-- Start adapter metadata
adapter : {
name: 'test',
add: 'addTest',
remove: 'deleteTest',
replace: 'updateTest',
load: {
procedure: 'getTests',
params: [],
key: 'peopleList'
}
}
//-- End adapter metadata
}
};
//Initialize
WL.JSONStore.init(collections)
.then(function () {
WL.Logger.debug("succes open");
caller.sync();
//handle success
})
.fail(function (errorObject) {
WL.Logger.debug("failed open");
//handle failure
});
}
调用的编辑方法(带有 pushrequest 和 push 用于测试目的):
caller.edit = function(){
var document = {_id: 3, json: {name: 'joopy', age: 666}};
var collectionName = 'people';
var options = {}; //default
WL.JSONStore.get(collectionName)
.replace(document, options)
.then(function () {
//handle success
WL.JSONStore.get(collectionName).getPushRequired()
.then(function (results) {
WL.Logger.debug(results);
WL.JSONStore.get(collectionName).push()
.then(function (res) {
//handle success
//res is an empty array if all documents reached the server
//res is an array of error responses if some documents failed to reach the server
})
.fail(function (errorObject) {
//handle failure
});
//handle success
})
.fail(function (errorObject) {
//handle failure
});
})
.fail(function (errorObject) {
//handle failure
});
}
以下是正确调用的适配器方法,但它需要一种方法来指定数据库中的某个记录。
function updateTest(data) {
//WL.Logger.error('Got data from JSONStore to REPLACE: ' + data);
var object = JSON.parse(data);
return WL.Server.invokeSQLStatement({
preparedStatement : updateStatement,
parameters : [object.name, object.age]
});
}
使用当前注释的 WL.Logger.error 显示此信息:
[错误] 从 JSONStore 获取数据到 REPLACE:{"name":"joopy","age":666} [project MyFirstApp]
我需要的是,数据的一种方式也可以为我提供旧数据或_id,或者另一个带有可用于指定数据库中记录的信息的参数。
【问题讨论】: