I want to parse a JSON file, then store its data in a variable and use it in different part of the code.
I think for this purpose I should use $.when().done(). However, I I do not know how to pass data.
The following code is not function but it shows what I am trying to achieve.
var myJSON;
function _parseJSON() {
$.getJSON(settings.json_src).done(function(data) {
return data;
});
}
$.when(_parseJSON()).done(function() {
myJSON = data;
});
function _cacheOptions() {
console.log(myJSON.data);
};
_cacheDom();
_events();
_render();
....
I need the following happen. 1- get JSON 2- save it in a variable. 3- having the variable available in the code to use its data.
At least the JSON data needs to be ready before few methods run.
Any idea?
Thanks in advance.
Well, you could use Promises for chaining instead, and use the data you pass around to do something with that data as you go through your chains.
So, if you want a sample implementation using promises, and handling passed around data, you can look at the following code, it:
Since a promise will wait for returned promises to complete (or fail), this all goes after each other, so the log statement will not be printed before the element got displayed.
When 1 step fails, it will jump to the next
catchin the chain. In my example, there is only 1 catch (and I am not handling errors from$.getJSON), so if something does go wrong, it should print an error in the console.You can of course make use of the
$.whenstatement provided by jQuery to do pretty much the same, as it returns aPromiseSo applied to your code, you should
returnthe$.getJSONfrom your parse function, and then chain based on thatThough I would really suggest not to work with global variables. If you add arguments to the functions you are calling, those functions will become independent from any magical variables, and can stand on their own instead
Note that if any of your chains return a deferred object, it will wait for that deferred object to be completed.
Note that we are not calling the methods ourselves in the chain, we just pass in the function reference, so it can be called in sequence ( so:
_eventsvs_events())