i have a poltergeist with this code:
javascript code:
var cache = {};
cache.init = function()
{
if(typeof(this.memory)=='undefined')
this.memory = {};
};
cache.set = function(key, value)
{
this.memory[key] = value;
};
cache.get = function(key)
{
if(console)
{
console.log("memory: ");
console.log(cache.memory);
console.log("key: "+key);
console.log(cache.memory[key]);
}
if(typeof(cache.memory[key])!='undefined')
return cache.memory[key];
return false;
};
var route = {};
route.load = function(url)
{
var route_list = cache.get('route_list');
if(route_list==false)
{
route_list = this.getList();
cache.set('route_list', route_list);
}
return route_list;
};
route.getList = function()
{
var result = false;
$.ajax(
{
url: 'route.json',
dataType: 'json',
async: false,
success: function(json)
{
result = json;
}
});
return result;
};
cache.init();
route.load();
route.load();
i would like to cache some objects in client side with "cache" set and get.
content of route.json file:
{
"/example":
{
"controller": "example"
},
"/example/show":
{
"controller": "example",
"method": "show"
}
}
and the result in firebug is:
memory:
Object
{
route_list
Object { /example={...}, /example/show={...}}
}
key: route_list
undefined
why is undefined? i can navigate inside the object with firebug o.O
EDIT: i added the second call of route.load(); that i get undefined key
i have a poltergeist with this code:
javascript code:
var cache = {};
cache.init = function()
{
if(typeof(this.memory)=='undefined')
this.memory = {};
};
cache.set = function(key, value)
{
this.memory[key] = value;
};
cache.get = function(key)
{
if(console)
{
console.log("memory: ");
console.log(cache.memory);
console.log("key: "+key);
console.log(cache.memory[key]);
}
if(typeof(cache.memory[key])!='undefined')
return cache.memory[key];
return false;
};
var route = {};
route.load = function(url)
{
var route_list = cache.get('route_list');
if(route_list==false)
{
route_list = this.getList();
cache.set('route_list', route_list);
}
return route_list;
};
route.getList = function()
{
var result = false;
$.ajax(
{
url: 'route.json',
dataType: 'json',
async: false,
success: function(json)
{
result = json;
}
});
return result;
};
cache.init();
route.load();
route.load();
i would like to cache some objects in client side with "cache" set and get.
content of route.json file:
{
"/example":
{
"controller": "example"
},
"/example/show":
{
"controller": "example",
"method": "show"
}
}
and the result in firebug is:
memory:
Object
{
route_list
Object { /example={...}, /example/show={...}}
}
key: route_list
undefined
why is undefined? i can navigate inside the object with firebug o.O
EDIT: i added the second call of route.load(); that i get undefined key
Share Improve this question edited Sep 24, 2012 at 11:20 ZiTAL asked Sep 24, 2012 at 10:55 ZiTALZiTAL 3,5819 gold badges37 silver badges51 bronze badges 2- Show us how you used firebug - I can't see any log statements – Bergi Commented Sep 24, 2012 at 11:02
- console.log is in cache.get, here the snapshot: i.imgur./VMSsp.png – ZiTAL Commented Sep 24, 2012 at 11:08
2 Answers
Reset to default 2OK, now I can see your issue. Your code
route.load();
route.load();
- tries to get the [uncached] value from the cache:
- this logs the memory object
- and logs
undefined
because there is nothing
- because it returned false
- get the JSON synchronously
- and set it in the cache
- again tries to get the [cached] value
- logs the memory object
- and logs the cached object
Everything is executed correctly. Now, you see the memory object two times in your console - but it is the same object, in its current state when inspecting it and not in the state when you logged it. See also
- How can I change the default behavior of console.log? (*Error console in safari, no add-on*)
- Bug in console.log?
- Wrong value in console.log
the error es from node.js:
socket.emit('zapp_client', '/example');
socket.emit('zapp_client', '/example/show');
i think it does asyncronously:
socket.emit('zapp_client', '/example');
setTimeout(function()
{
socket.emit('zapp_client', '/example/show');
}, 1000);
with this i solved the problem temporarily...