views:

6067

answers:

3

I am submitted a getJSON request to a controller in my application, this controller is returning valid JSON with 2 "applications." I know this for a fact as if I move the alert statement to within jQuery's each function it will give me the expected result.

I am attempting to store this data within a multidimensional array to later be used with 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 Response:

{"applications":[{"slug":"test","title":"Test"},{"slug":"hardware","title":"Hardware"}]}

Expected Result:

Test

Actual Result (from Firebug):

applicationList[0] is undefined

If I replace the alert() above, with the following code I get one alert window with the text "remove":

for (p in applicationList) {
    alert(p);
}

Now, my thinking is that the JSON request isn't completing in-time for the alert() so I'll use a named callback function to ensure the request has completed:

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 is telling me that data is undefined...

I feel like I am almost there, but I've been almost there for the past hour and I feel as if I am just polluting the source now in trying to get it to work.

+4  A: 

This should do it:

Ext.onReady(function() {
    var applicationList = [];
    var applicationMenu;

    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'];
        });

        applicationMenu = Ext.menu.Menu({
            items: applicationList
        });
    });
});

Your thinking is right; the reason it is not working is because AJAX is an asynchronous process and as you fire off the getJSON function javascript keeps on trucking along. Your solution doesn't work because making it a named callback is not changing the fact that it won't be fired until you've already tried to initialize the Menu. All my solution is doing is moving the Menu initilization code INSIDE the callback, as it is then and only then that you will have access to the filled out applicationList.

Paolo Bergantino
the "applicationList" nd "applicationMenu" variable definitions can (and probably should) be moved into your getJSON anonymous function.
Matt Huggins
+3  A: 

You're right, you should use your "applicationList" variable only after the getJSON callback has finished.

You should call Ext.menu.Menu() inside your getJSON callback, after jQuery.each().

Seb
A: 

function json(){

  var url="http://192.172.2.23:8080/geoserver/wfs?request=GetFeature&version=1.1.0&outputFormat=json&typeName=topp:networkcoverage&CQL_FILTER= topp:CELL_ID='410-07-301-31781' Or topp:CELL_ID='nnn'&callback=?";
  jQuery.getJSON(url,function(data)
   {
    alert("received");
    alert("Symbol: " + data.type + ", Price: " + data.bbox);
  });




  }

this code is giving the following error :

Error: invalid label Source File: http://192.172.2.23:8080/geoserver/wfs?request=GetFeature&version=1.1.0&outputFormat=json&typeName=topp%3Anetworkcoverage&CQL%5FFILTER=%20topp%3ACELL%5FID=%27410-07-301-31781%27%20Or%20topp%3ACELL%5FID=%27nnn%27&callback=jsonp1254383189886&%5F=1254383191295 Line: 1, Column: 1 Source Code: {"type":"FeatureCollection","features":[{"type":"Feature","id":"networkcoverage.13333","geometry":{"type":"MultiPolygon","coordinates":[[[[33.67787000000004,73.02342000000004],[33.68024256600006,73.02193745600005],[33.68066767800008,73.02342000000004],[33........

any idea wot might be wrong,,

Imran