2009-08-12 33 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回調函數,而不是與您的函數內聯。