【问题标题】:JSONStore + adapter pushing replace - how to give _id as a parameter?JSONStore +适配器推送替换-如何将_id作为参数?
【发布时间】: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,或者另一个带有可用于指定数据库中记录的信息的参数。

【问题讨论】:

    标签: ibm-mobilefirst jsonstore


    【解决方案1】:

    阅读文档的Working with external data 部分。基本上,您可以代替WL.JSONStore.get(collectionName).push()

    var accessor = WL.JSONStore.get('people');
    
    accessor.getAllDirty()
    
    .then(function (dirtyDocs) {
      // ...
    });
    

    dirtyDocs 参数类似于以下示例:

    [{_id: 1,
      json: {id: 1, ssn: '111-22-3333', name: 'Carlos'},
      _operation: 'add',
      _dirty: '1395774961,12902'}]
    

    我需要的是,数据的一种方式也可以为我提供旧数据或_id,或者另一个带有可用于指定数据库中记录的信息的参数。

    你将拥有_id,然后你可以这样做:

    .then(function (dirtyDocs) {
    
      return WL.Client.invokeProcedure({
        adapter : 'people',
        procedure : 'updatePeople',
        parameters : [ dirtyDocs ]
      });
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 2016-12-22
      • 2017-07-11
      • 2019-06-12
      • 2020-02-02
      • 2020-07-03
      • 2019-03-31
      相关资源
      最近更新 更多