【发布时间】:2016-08-12 04:10:25
【问题描述】:
我正在尝试在 Mobilefirst 7.0 混合应用程序中创建 JSONStore。我遇到的问题是能够在应用程序的后续运行中检测到 JSONStore 已经存在。文档说您必须在调用 WL.JSONStore.get(...) 之前调用 WL.JSONStore.init(...)。
所以问题是,在应用程序的后续运行(意味着应用程序第一次运行并成功创建 JSONStore)并且这是一次新运行时,检查 JSONStore 是否已存在的正确方法是什么?
如果我必须再次调用 init,我该如何在不清除其中的内容的情况下这样做?
我目前正在使用这个sn-p的代码来检测...
function checkJSONStore() {
alert("In checkJSONStore");
var collectionName;
try {
// Check to see if JSONStore exists...
collectionName = WL.JSONStore.get('appStore');
} catch (e) {
// TODO: handle exception
alert("checkJSONStore: Exception = " + e.message);
}
alert("Returning from checkJSONStore: " + collectionName);
return collectionName;
}
这是创建商店的代码......它运行成功。
function initJSONStore() {
console.log("In initJSONStore:");
var collectionName = "appStore";
var Data = {
item: 'newinstall',
value: 1
};
var JSONStoreCollections = {};
JSONStoreCollections[collectionName] = {};
JSONStoreCollections[collectionName].searchFields = {item: 'string'};
try {
console.log("Destroy any collections before we start");
WL.JSONStore.destroy().then(function () {
//handle success
console.log("initJSONStore: JSONStore destroy success");
})
.fail(function (error) {
//handle failure
console.log("initJSONStore: JSONStore destroy failure: " + error);
});
console.log("Calling WL.JSONStore.init");
WL.JSONStore.init(JSONStoreCollections).then(function () {
console.log("initJSONStore: JSONStore init success");
WL.JSONStore.get('appStore').add(Data).then(function () {
console.log("initJSONStore: JSONStore add success");
}).fail(function (error) {
console.log("initJSONStore: JSONStore add failure: " + error);
});
}).fail(function (error) {
console.log("initJSONStore: JSONStore init failure");
});
} catch (e) {
// TODO: handle exception
//console.log("initJSONStore: Exception = " + e.message);
alert("initJSONStore: Exception = " + e.message);
}
console.log("Exiting initJSONStore:");
}
【问题讨论】: