【问题标题】:How to pull data from SqlDataAdapter and store it in a JsonStore collection in MobileFirst如何从 SqlDataAdapter 中提取数据并将其存储在 MobileFirst 中的 JsonStore 集合中
【发布时间】:2015-09-24 11:17:02
【问题描述】:

我有一个SqlDataAdapter,我想将它存储在 MobileFirst 的 JsonStore 集合中,并以表格形式显示。我尝试过使用Load() 方法,但它不起作用。 这是我的 resultSetCollection.js 文件

;(function () {

WL.JSONStore.init({
    resultSet : {
        searchFields: {"EMP_NAME":"string","EMP_ID":"integer"}
    }
}, {
    // password : 'PleaseChangeThisPassword'
})

.then(function () {

     return WL.Client.invokeProcedure({
          adapter : 'EmployeeList',
          procedure : 'getEmployeeLists',
          parameters : []
        }); 
})
.then(function (responseFromAdapter) {

     alert('responseFromAdapter:' + JSON.stringify(responseFromAdapter.invocationResult.resultSet));
     var accessor = WL.JSONStore.get('resultSet');
     var data=responseFromAdapter.invocationResult.resultSet;
     var changeOptions = {
                replaceCriteria : ['EMP_ID', 'EMP_NAME'],
                addNew : true,
                markDirty : false
              };

    return accessor.change(data, changeOptions);
})
.then(function (response) {
    console.log(response);
    //Here I want to retrieve the collection and display it in a table
})
.fail(function (errObj) {
    WL.Logger.ctx({pretty: true}).error(errObj);
});

}());

【问题讨论】:

  • 向我们展示一些代码!!如果您没有向我们展示任何内容,我们到底应该如何帮助您!?!??!?!?我们无法读取您的屏幕 - 也无法读取您的想法 - 您必须向我们展示您拥有的东西并解释您正在做什么以及什么不起作用

标签: ibm-mobilefirst jsonstore


【解决方案1】:

来自客户端应用程序的适配器过程请求将在其成功和失败回调中包含一个响应对象。因此,假设请求成功并且数据已从后端服务器返回。

我们还假设您已经初始化了一个 JSONStore 并正确设置了一个集合。然后,您只需要获取集合并向其中添加数据。

以下示例从 HTTP 适配器请求中获取完整响应,并将其按原样放入集合中。您当然需要为您的特定场景创建更好的设置...

请注意,代码未优化,性能或逻辑 100% 正确。这只是一个演示流程。

在 MobileFirst Platform Foundation 7.0.0.00 中测试。
ma​​in.js:

var collectionName = 'mydata';
var collections = {
    mydata : {
        searchFields : {data: 'string'},
    }
};

function wlCommonInit(){
    WL.JSONStore.init(collections).then(
        function() {
            var resourceRequest = new WLResourceRequest("/adapters/myadapter/getStories", WLResourceRequest.GET);
            resourceRequest.send().then(resourceRequestSuccess, resourceRequestFailure);
        }
    );  
}

function resourceRequestSuccess(response) {
    WL.JSONStore.get(collectionName).add(response).then(
        function(){
            WL.Logger.info("successfully added response to collection");
            displayDataFromCollection();
        }, 
        function() {
            alert("failed adding response to collection");
        }
    );
}

function resourceRequestFailure() {
    alert ("failure");
}

如果您想从 JSONStore 获取数据并将其显示在 HTML 中,您可以执行以下操作:

// get a specific item from the stored response and display it in a table
function displayDataFromCollection() {
    WL.JSONStore.get(collectionName).findAll().then(
        function(result) {
            $("#mytable").append("<tr><td>" + result[0].json.responseJSON.rss.channel.title + "</td></tr>");
        }, 
        function() {
            alert ("unable to display collection");
        }
    );
}

index.html 如下所示:

<table id="mytable">
</table>

【讨论】:

  • 如何以表格格式显示这个集合,如果我想调用 Sql Adapter 而不是 Http Adapter 怎么办?
  • 您不将集合放在表格中,而是在表格中显示响应。您需要从集合内部获取数据 - 此数据、来自适配器的响应......是一个 JSON 对象,因此您只需选择您想要显示的特定数据,并使用普通的常规 JavaScript 附加它HTML 中所需的元素。找时间学习 JavaScript 以及阅读 JSONStore 文档。
  • @HyderAli,如果此答案对您有帮助,请标记为已回答。
猜你喜欢
  • 2012-01-29
  • 2021-08-20
  • 2014-02-06
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 2020-12-16
  • 2015-10-06
  • 2017-04-06
相关资源
最近更新 更多