2009-08-12 34 views
0

当提交表单时,我调用一个函数getPosts并传递一个变量str。我想要做的是获取从该函数返回的数据。从函数返回的访问数组 - javascript/jquery noob moment

// when the form is submitted 
$('form#getSome').submit(function(){ 
    var str = $("form#getSome").serialize(); 
    var something = getPosts(str); 

    * This is where I'd like to get the data returned from getPosts() 

    return false; 
}); 

// get the data 
    function getPosts(str){ 

     $.getJSON('http://myurl.com/json?'+str+'&callback=?', 
      function(data) { 

      arrPosts = new Array(); 

       $.each(data.posts, function(i,posts){ 

       // build array here 

       }); 

       return arrPosts; 

      }); 
    }; 

我试过很多东西,但只得到“未定义”返回。我试过console.log(某事); console.log(getPosts)。

我在这里错过了一些非常基本的东西。任何帮助将不胜感激。

编辑:

我试图做的是建立一个单一的功能会得到职位。然后不同的事件会调用该函数。然后我可以使用这些数据。所以一个事件可能会提交一个表单,另一个可能会点击一个链接,另一个懒惰/无尽的滚动。所有人都可以使用相同的getPosts功能。

有很多解析结果,这相当于很多代码行。只是试图找到一种方法来重用该功能。你认为那可能吗?

$('a.thisLink').click(function(){ 
    getPosts(); 
    get the return from getPosts() and do something with it 
}); 

$('form.thisForm').submit(function(){ 
    getPosts(); 
    get the return from getPosts() and do something with it 
}); 

function getPosts(){ 
    get the posts and return an array 
} 

回答

4

Ajax请求的异步执行,回调函数(function (data))被执行时,请求结束,并且返回该回调中的值不起作用,因为它是getPosts中的嵌套函数,并且其返回值从不使用。

其实在你的例子中,getPosts不返回任何东西,它在之前结束它的执行返回数据。

我建议你对你的工作提交事件处理程序,如果你想保持getPosts功能,您可以引入一个回调参数:

$('form#getSome').submit(function(){ 
    var str = $("form#getSome").serialize(); 

    getPosts(str, function (data) { 
    var array = []; 
    $.each(data.posts, function(i,posts){ 
     // build array here 
     array.push(/* value to add */); 
    }); 
    // array ready to work with... 
    //... 
    }); 
    return false; 
}); 

function getPosts(str, callback){ 
    $.getJSON('http://myurl.com/json?'+str+'&callback=?', callback); 
} 

编辑2:针对您的第二条评论,您可以进行另一次回调,当数据由第一个回调处理时将执行该回调,并且您可以在提交事件处理函数上执行getPosts函数时对其进行定义:

$('form#getSome').submit(function(){ 
    var str = $("form#getSome").serialize(); 

    getPosts(str, reusableCallback, function (result) { 
    // result contains the returned value of 'reusableCallback' <--- 
    }); 
    return false; 
}); 

function reusableCallback(data) { 
    var array = []; 
    $.each(data.posts, function(i,posts){ 
    array.push(/* value to add */); 
    }); 
    //... 
    return array; 
} 

function getPosts(str, callback, finishCallback){ 
    $.getJSON('http://myurl.com/json?'+str+'&callback=?', function (data) { 
    finishCallback(callback(data)); // pass the returned value 
            // of callback, to 'finishCallback' which is 
            // anonymously defined on the submit handler 
    }); 
} 

编辑3:我认为getPosts功能和“reusableCallback”的功能有很大的关系,你可能想加入他们的行列,使代码更易于使用和理解:

$('form#getSome').submit(function(){ 
    var str = $("form#getSome").serialize(); 

    getPosts(str, function (result) { 
    // result contains the processed results 
    }); 

    return false; 
}); 


function getPosts(str, finishCallback){ 
    $.getJSON('http://myurl.com/json?'+str+'&callback=?', function (data) { 
    // process the results here 
    var array = []; 
    $.each(data.posts, function(i,posts){ 
     array.push(/* value to add */); 
    }); 
    //... 
    finishCallback(array); // when the array is ready, execute the callback 
    }); 
} 
+0

我认为这是正确的轨道。我想要做的是创建一个可以获取帖子的函数。然后不同的事件会获得帖子。所以一个事件可能会提交一个表单,另一个可能会点击一个链接,另一个懒惰的滚动。所有人都可以使用相同的getPosts功能。有很多解析结果,这相当于很多代码行。只是试图找到一种重用方法。你认为那可能吗? – jyoseph 2009-08-12 04:24:54

+0

是的,这正是我正在寻找的,这样我可以不断重复使用reusableCallback函数。 最后一个问题,我如何引用在submit函数中返回的数据(来自reusableCallback函数)? – jyoseph 2009-08-12 05:00:14

+0

@jyoseph:看看我的第二个编辑... – CMS 2009-08-12 05:39:14

0

你getPosts功能不全看,我不是jQuery的专家,但它应该是这个样子:的getJSON的

function getPosts(str) { 

    $.getJSON('http://myexample.com/json?'+str+'&callback=?',function(data){ 

    var arrPosts = []; 

    $.each(data.posts, function(i,posts){ 
    ... build array yada yada ... 
    }); 

    return arrPosts; 

    }); 
} 
0

问题在于get请求返回数据时调用了$ .getJSON回调函数,而不是与您的函数内联。