I am using the Mobile First Platform Studio plugin 7.1 with Eclipse and following JSONStore Tutorial.
I understand from this posting that the function calls are asynchronous. It seems that functions such as add, delete, find etc can only be done in the then() immediately following the initialize. In this example, it works fine in that sense but as soon as I do something out of the then() following the initialize, I get an error "PERSISTENT_STORE_NOT_OPEN"
function wlCommonInit() {
WL.JSONStore.destroy();
var collectionName = 'people';
var collections = {};
collections[collectionName] = {};
collections[collectionName].searchFields = {
name: 'string',
age: 'integer'
};
var options = {};
WL.JSONStore.init(collections, options)
.then(function() {
WL.Logger.debug("Collections " + collectionName + " initialized");
var data = [
{name: 'Trevor',age: 30},
{name: 'Allison',age: 28},
{name: 'Peyton',age: 28}
];
var dataOptions = {};
WL.JSONStore.get(collectionName).add(data, dataOptions)
.then(function() {
WL.Logger.debug("Data added to the collections");
var query = {
name: 'Peyton'
};
var options = {};
WL.JSONStore.get(collectionName).find(query, options)
.then(function(result) {
WL.Logger.debug("Found: " + result[0]._id + " " + result[0].json.name);
}).fail(function(result) {
WL.Logger.error("FIND FAILED: " + result);
});
}).fail(function() {
WL.Logger.error("Data failed to add to the collections");
});
}).fail(function() {
WL.Logger.error("Collections " + collectionName + " failed to initialize");
});
// added outside the then()
var query = {_id: 2};
var options = {exact: true, limit: 1};
WL.JSONStore.get(collectionName).find(query, options)
.then(function(result) {
WL.Logger.debug("Found: " + result[0]._id + " " + result[0].json.name);
}).fail(function() {
WL.Logger.error("FIND BY ID FAILED");
});
}
My question is how to I cater to a button click and then add data to the JSONStore? That is how can I get access to the store again outside the first then()
?
In the code I have a comment //Added outside the then()
where the error in question is thrown.