I have a JSON file has information and data about my work like this :
{
"Employes":[
{
"id": 1,
"fullName": "Test Test"
}
],
"Infos":[
{
"id": 1,
"address": "Test Test test test test",
"employes": 1
}
]
}
I want to generate Employes and Infos Classes automatically on JS code and the add some method to it.
fetchJSONFile it's a function to get Data from JSON file using AJAX:
function fetchJSONFile(callback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
var data = JSON.parse(httpRequest.responseText);
if (callback) callback(data);
}
}
};
httpRequest.open('GET', 'datas.json');
httpRequest.send();
}
So, here on generate function I want to generate Classes automaticaly and assign object to it, I try by doing this:
function generate(nameOfObject){
fetchJSONFile(function(data){
employes = Object.assign(new Employes(), ...data[nameOfObject]);
console.log(employes);
});
}
On this line I assign JSON object to My Employes() classes, my question is how to generate Employes() automatically assign of JSON Type, so if is Infos for example new Employes() become new Infos() ... etc.
I want to do that, to add some functions to those Classes, like addNew(), deleteOne() .... etc, All about CRUD.
Is there any solution ?