In my experiment till date, I'm still staying with local storage & not using data store for two primary reasons:
- "Set" data from server connect: When using a local storage, I can easily set the results of a server connect to the local storage (both same schema) with a single liner like this:
{{local_storage.set('search_config',search_config.data.search_config)}}
while with data store, looks like this "Set" function is missing, and @patrick has given a workaround for this here - Set Data Store Value/Array from query in Server Connect - #7 by patrick. I would have thought data store being an abstraction over local storage didn't lose this simple no-brainer set function.
- "Post" data to server connect: I'm aware of the hidden field post method, thanks to @ben's amazing tutorials. The problem is, the moment you have more than one level in your data store (array of objects), the hidden field method becomes too cumbersome. I have spent hours making this work when I wanted to post a nested JSON by passionately following @nevil's thread here - Send nested JSON with server-side API
When we talk about "post"ing data store, let's not forget that we might need to post nested schema as well, in which case we are in fact posting "application/json" type.
Later, I just used the below javascript snippet, called it (from a static event) when I had to post nested json back to server, and I got rid of all the multi-dimensional array of hidden form fields.
function post_search_config(event) {
// Sending and receiving data in JSON format using POST method
var xhr = new XMLHttpRequest();
var url = "http://localhost/dmxConnect/api/userprofile/search_config.php";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
var search_config = dmx.parse('local_storage.data');
var data = JSON.stringify(search_config);
xhr.send(data);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
// things to do after getting a response for the json post
dmx.parse('search_orders.load()');
dmx.parse('flow_set_advanced_filtersort.run()');
}
};
}
Anyone wanting to post data store to server connect could use the above js snippet as an alternative to the hidden form fields method!