I have this erratic behavior: I have an object, I extend its property and initalize it. I do a callback to populate the property. Then, if I check the property outside the callback, it results as initialized; but if I check the whole object,also the property is updated.
Am I doing something wrong in updating my property?
Am I doing smtg conceptually wrong with local variables?
I update my property with this function:
function extend(obj, props) {
for(var prop in props) {
if(props.hasOwnProperty(prop)) {
obj[prop] = props[prop];
}
}
};
And then:
graphics.node(function (node) {
extend(node,{
thumbnail : {
source: '',
width: '',
height: ''
}
});
// UI
if (typeof node.data === 'undefined') {
var nameTopic = 'NOT YET MAPPED'; // YOU DONT NEED THIS INFO
var sourceId = 'ID NOT YET MAPPED' // YOU DONT NEED THIS INFO
} else {
var nameTopic = node.data.name;
var sourceId = node.data.source;
// I POPULATE MY PROPERTY WITH FOLLOWING FUNCTION: node.thumbnail...
fetchWikipediaDecorator(sourceId,function(data){
var infopage = data.query.pages[sourceId];
if (infopage.thumbnail) {
node.thumbnail.source = infopage.thumbnail.source;
node.thumbnail.width = infopage.thumbnail.width;
node.thumbnail.height = infopage.thumbnail.height;
}
});
// NOW ODD THING IS:
console.log('registro il source',node.thumbnail);
// RESULT: {source:'',width:'',height:''}
// INSTEAD
console.log('registro il source', node);
// RESULT WITH UPDATED PROPERTIES FOR ALL THE OBJECT
// {...,thumbail{source:'http...',etc.}, ..}
}
// SO BASICALLY I CANT CALL THE PROPERTY..