0

Here is the "problem", I fill PHP objects with data like this :

public function hydrate(array $data){
    foreach($data as $key=>$value){
        $method = 'set'.ucfirst($key);
        if(METHOD_EXISTS($this,$method)){
            $this->$method($value);
        }
    }
}

Now, i'd like to turn it to javascript object instead of PHP, so, i get the "data array" from my db with an .ajax call, and I get a JSON object that"looks" like this :

{"id":"44","name":"test","testKey":"qklfjmdjfq88"}

My Javascript "object" looks like this :

function myObject(){
    var element,id,name,testKey;

    this.hydrate = function(ajaxData){
        //THIS CODE DOES NOT WORK
        //THAT IS THE PROBLEM
        Object.keys(ajaxData).forEach(function(key){
            console.log("function loop key : "+key);//OK
            console.log("function loop value : "+ajaxData[key]);//OK
            this.key = Element[key];
        });
    };
}

so, once I receive my JSON "array" in my js script, i'd like to turn it to a js object instance, so I'm doing this :

var myCurrentObject = new myObject();
myCurrentObject.hydrate(/*WHAT MY AJAX CALL RETURN*/);
console.log(myCurrentObject.name);
//console shows undefined...

So problem seems to be in the .hydrate function... this.key does not correspond to the myObject instance var... (name / id / testKey) How to fill the js object instance with a function that looks like the one I use when working with php?

Thanks for help/reading

1
  • You may find that your "ajaxdata" is already an object, try logging it to the console to check. If you then find that it is actually a string then try using JSON.parse to turn it into an object. See here: developer.mozilla.org/en/docs/Web/JavaScript/Reference/… Commented Jan 14, 2015 at 9:02

1 Answer 1

4

In this function

this.hydrate = function(ajaxData){
    Object.keys(ajaxData).forEach(function(key){
        console.log("function loop key : "+key);
        console.log("function loop value : "+ajaxData[key]);
        this.key = Element[key];
    });
};

this.key is not referring to myObject, you should try:

this.hydrate = function(ajaxData){
    Object.keys(ajaxData).forEach(
      // Execute a function passing param this as obj that return a function 
      // (closure)
      (function(obj){
        return function(key){
            obj[key] = ajaxData[key];
        };
      })(this)
    );
};

or save it in a variable

this.hydrate = function(ajaxData){
    var that = this;
    Object.keys(ajaxData).forEach(function(key){
        that[key] = ajaxData[key];
    });
};

or simplier use a for loop

this.hydrate = function(ajaxData){
    var k =  Object.keys(ajaxData);
    for(var i=0, len = k.length; i<len; i++){
        this[k[i]] = ajaxData[k[i]];
    }
};
0

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.