2

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 ?

1

2 Answers 2

2

You could create a DynamicClass class which accepts an input value and type which could be Employes, Infos...

function DynamicClass (data, type) {
  this.type = type;
  // Init function
}
DynamicClass.prototype.xxxx = function () {}

You can now just use your data object to create the classes.

fetchJSONFile(function(data){
  for(var key in data) {
    var classObj  = new DynamicClass(data[key], key);
  }
});
Sign up to request clarification or add additional context in comments.

Comments

2

If the object returned has only 2 keys you could map through the entries and create 2 array variables like this:

If you have more then 2 properties, then you can use a switch inside the inner map:

function Employee() { this.defaultEmployeeProp = "default" }
function Infos() { this.defaultInfosProp = "default" }

const data={"Employes":[{"id":1,"fullName":"Test Test"}],"Infos":[{"id":1,"address":"Test Test test test test","employes":1}]}

const [employees, infos] = Object.entries(data).map(([key, values]) =>
  values.map(e => Object.assign(key === "Employes" ? new Employee() : new Infos(), e))
)

console.log(employees)
console.log(infos)

If you want all types of objects to have the same prototype, then there is no need for a ternary inside the map. Create a generic constructor function or a class, then create instances of the class. If you want specific behavior for each type of object, you can always extend the class and use the switch as mentioned in the previous snippet

function GenericConstructor() {
  this.default = "default"
}

GenericConstructor.prototype.commonMethod = function() {
  console.log("common method called")
}

const data={"Employes":[{"id":1,"fullName":"Test Test"}],"Infos":[{"id":1,"address":"Test Test test test test","employes":1}]}

const [employees, infos] = Object.entries(data).map(([key, values]) =>
  values.map(e => Object.assign(new GenericConstructor(), e))
)

console.log(employees)
console.log(infos)

6 Comments

The problem here sir, is if I add new type on JSON file, I need to create the Classe manually. I want to generate classes automatically
Does the class of all the types have the same prototypes? @sayou
@varunagarwal, on this moment all those classes has the same methods, (just CRUD)
@sayou If all the items have the same behavior, create a generic class and then Object.assign(new GenericClass(), e)?
@adiga can you please give an example how to use it ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.