Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating JS Object with methods from JSON

I need the following object:

var myObj={
    "rules": {
        "email": {
            "required": true,
            "email": true,
            "remote": {
                "url": "check-email.php",
                "type": "post",
                "data": {
                    "someName1": function() {
                        return $( '#someID1' ).val();
                    },             
                    "someName2": function() {
                        return $( '#someID2' ).val();
                    },             
                    "someName3": "bla"
                }
            }
        }
    }
};

To create it, I only have some JSON such as the following:

{
    "rules": {
        "email": {
            "required": true,
            "email": true,
            "remote": {
                "url": "check-email.php",
                "type": "post",
                "data": {
                    "someName1": {"id":"someID1"},             
                    "someName2": {"id":"someID2"},             
                    "someName3": "bla"
                }
            }
        }
    }
}

or

{
    "rules": {
        "email": {
            "required": true,
            "email": true,
            "remote": {
                "url": "check-email.php",
                "type": "post",
                "data": {
                    "someName1": {"function":"return $( '#someID1' ).val()"},             
                    "someName2": {"function":"return $( '#someID2' ).val()"},             
                    "someName3": "bla"
                }
            }
        }
    }
}

or something similar.

How can this be accomplished?

like image 616
user1032531 Avatar asked Jul 05 '26 15:07

user1032531


2 Answers

You can use Function to create functions on the fly. Supposing you have the second JSON structure:

d = obj.rules.email.remote.data;
Object.keys(d).forEach(function(key) {
    d[key] = new Function(d[key].function); 
    //Note: You might want to check if d[key].function exists before using it.
});
like image 94
UltraInstinct Avatar answered Jul 07 '26 06:07

UltraInstinct


For this pattern :

{
    "rules": {
        "email": {
            "required": true,
            "email": true,
            "remote": {
                "url": "check-email.php",
                "type": "post",
                "data": {
                    "someName1": {"id":"someID1"},             
                    "someName2": {"id":"someID2"},             
                    "someName3": "bla"
                }
            }
        }
    }
}

Can be done as:

for(var key in myObj.rules.email.remote.data){
    if(myObj.rules.email.remote.data[key].id){
    myObj.rules.email.remote.data[key] = function(){
          return $('#'+myObj.rules.email.remote.data[key].id).val();
        }
    }
}

if email is not hardcoded then it can be done as:

for(var key1 in myObj.rules){
 for(var key in myObj.rules[key1].remote.data){
        if(myObj.rules[key1].remote.data[key].id){
        myObj.rules[key1].remote.data[key] = function(){
              return $('#'+myObj.rules[key1].remote.data[key].id).val();
            }
        }
    }
}
like image 33
Gaurav Singla Avatar answered Jul 07 '26 04:07

Gaurav Singla