I was sent a getJSON request to the controller in my application, this controller returns a valid JSON with 2 "applications". I know this as a fact, as if I moved the warning statement to jQuery every function, it would give me the expected result.
I am trying to store this data in a multidimensional array so that I can use it later using the extJS menu control.
code:
Ext.onReady(function() { var applicationList = []; jQuery.getJSON('index.php/applications', function(data) { jQuery.each(data.applications, function (i, app) { applicationList[i] = []; applicationList[i]['text'] = app['title']; applicationList[i]['id'] = app['slug']; }); }); alert(applicationList[0]['text']); var applicationMenu = Ext.menu.Menu({ items: applicationList }); });
JSON answer:
{"applications":[{"slug":"test","title":"Test"},{"slug":"hardware","title":"Hardware"}]}
Expected Result:
Test
Actual result (from Firebug):
applicationList [0] - undefined
If I replaced alert() above with the following code, I get one warning window with the text "remove":
for (p in applicationList) { alert(p); }
Now, I think the JSON request does not complete on time for alert() , so I will use the callback function with the name to make sure the request is complete:
var data; jQuery.getJSON('index.php/applications', get_applications(data)); function get_applications(data) { jQuery.each(data.applications, function (i, app) { applicationList[i] = []; applicationList[i]['text'] = app['title']; applicationList[i]['id'] = app['slug']; }); };
But now Firebug tells me that data is undefined ...
It seems to me that I am almost there, but I have been almost there for the last hour, and it seems to me that I am just polluting the source now, trying to make it work.