多维数组和jQuery的getJSON [英] Multidimensional Arrays and jQuery's getJSON

查看:84
本文介绍了多维数组和jQuery的getJSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向我的应用程序中的控制器提交了一个getJSON请求,该控制器返回带有2个应用程序的有效JSON。我知道这个事实好像我将警告语句移动到jQuery的每个函数中它会给我预期的结果。

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.

我试图将这些数据存储在一个多维数组稍后将与extJS'菜单控件一起使用。

I am attempting to store this data within a multidimensional array to later be used with extJS' Menu Control.

代码:

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回应:

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

预期结果:


测试

Test

实际结果(来自Firebug):

Actual Result (from Firebug):


applicationList [0]未定义

applicationList[0] is undefined

如果我替换 alert()以上,使用以下代码我得到一个警告窗口,其中包含文本remove:

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

现在,我的想法是JSON请求未及时完成对于 alert()所以我将使用命名回调函数来确保请求已完成:

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

但现在Firebug告诉我数据未定义 ...

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.

推荐答案

这应该这样做:

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

你的想法是对的;它不起作用的原因是因为AJAX是一个异步过程,当你触发getJSON函数时,javascript继续运行。您的解决方案不起作用,因为将其作为命名回调并不会改变这样的事实,即在您尝试初始化菜单之前不会触发它。我的所有解决方案都是将菜单启动代码移动到回调中,因为只有这样才能访问已填写的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.

这篇关于多维数组和jQuery的getJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆