Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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

share|improve this question
    
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/… – N.R Jan 14 at 9:02
up vote 3 down vote accepted

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]];
    }
};
share|improve this answer
1  
You've just take this from my tongue :) – proxyfabio Jan 14 at 9:09
    
Perfect! "simplier loop" is exactly what I was looking for :) thanks! – Julo0sS Jan 14 at 9:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.