$ .get JSON请求+填充并返回ARRAY

时间:2011-12-31 17:51:39

标签: javascript jquery

我开始使用移动框架LungoJS。我和javascript工作不太好,但我真的想修改这个原始代码:

ORIGINAL.JS

var mock = function() {
        var mock = [];
        for (var i=1; i<=5; i++){
            mock.push({
                id: i,
                name: 'name n'+i,
                description: 'description n'+i
            })
        }
        lng.View.Template.List.create({
            container_id: 'lives',
            template_id: 'show_music_template',
            data: mock    
        })
    }
    return {
        mock: mock
    }



})(LUNGO, App);

这个原始代码工作正常并且很容易,现在我想使用$ .get返回JSON文件并填充数组,如ORIGINAL.JS:

JSON结果:

    {"result":[
    {"id":"52","username":"jgali","image":"Prova_(live)387.jpeg","name":"Prova (live)","type":"music","language":"Catalan","category":"8","tags":"indie, dine prova, indie live","description":"Aquesta es una prova online de reidiou","licence":"Reidiou License","played":"54","record_time":"45","facebook_id":"1052266203_2342869925158","twitter_hash":"#Provalive","create_date":"2011-11-01 13:04:21"},
    {"id":"52","username":"jgali","image":"Prova_(live)387.jpeg","name":"Prova (live)","type":"music","language":"Catalan","category":"8","tags":"indie, dine prova, indie live","description":"Aquesta es una prova online de reidiou","licence":"Reidiou License","played":"54","record_time":"45","facebook_id":"1052266203_2342869925158","twitter_hash":"#Provalive","create_date":"2011-11-01 13:04:21"}
]}

SERVICE.JS

var mock = function() {
        var mock = [];
        var url = 'http://localhost/app/rest/podcasts';
        var data = {};

        //lng.Service.get = $get
        lng.Service.get(url, data,function(response) { 
            var array = [];
            //Do something with response
             jQuery.each(response.result, function() {
                    mock.push({
                        id: this.id,
                        name: this.name,
                        description: this.description
                    })    
            });
            document.write(mock[1].id);
        });
        lng.View.Template.List.create({
            container_id: 'lives',
            template_id: 'show_music_template',
            data: mock  
        })  
    }
    return {
        mock: mock
    }

问题是外部循环我不能使用“mock”数组。当然我犯了几个错误......但是有人知道这是什么问题吗?

感谢。

2 个答案:

答案 0 :(得分:0)

问题是$.get()需要时间来执行,因此是异步的。像这样的异步调用涉及使用callback函数。要访问mock数组,您需要在此回调中嵌套任何内容。

你也可以强制AJAX调用在jQuery中同步(尽管我和文档警告过这个);根据{{​​3}}:

  

默认情况下,所有请求都是异步发送的(即设置为   默认为true)。如果需要同步请求,请将此选项设置为   假。跨域请求和dataType:“jsonp”请求没有   支持同步操作。请注意,同步请求可能   暂时锁定浏览器,在请求时禁用任何操作   很活跃。

答案 1 :(得分:0)

谢谢!正如你所说,我使用回调解决了这个问题。

如果有兴趣的话,我发布代码:

App.Services = (function(lng, app, undefined) {

var mock = function() {
        var mock = new Array();
        var url = 'http://localhost/app/rest/podcasts';
        var data = {};

        function getData (url,data,mock,callbackFnk){
            lng.Service.get(url, data,function(response) {
                //Do something with response
                // now we are calling our own callback function
                if(typeof callbackFnk == 'function'){
                  callbackFnk.call(this, response);
                }else{
                    document.write("Error");   
                }

            });
        }
        getData(url,data,mock,function(response){

            jQuery.each(response.result, function() {
                    mock.push({
                        id: this.id,
                        name: this.name,
                        description: this.description
                    })

            });

            lng.View.Template.List.create({
            container_id: 'lives',
            template_id: 'show_music_template',
            data: mock
            })
        })     
    }

    return {
        mock: mock
    }

})(LUNGO, App);